如何将一个组合框的结果绑定到另一个组合框中在Visual Studio(C#/SQL)中。

huangapple go评论73阅读模式
英文:

How to bind the results of one combobox to another combobox in Visual Studio (C#/SQL)

问题

以下是翻译好的部分:

我正在创建一个用于显示车主及其相应车辆的界面(类似虚拟的DMV)。

我有一个绑定到数据库的组合框,它会自动填充一些文本字段(如名字、姓氏等),还有一个用于填充车辆信息(制造商、型号等)的第二个组合框,但我在尝试让第二个组合框仅显示与第一个组合框中的ownerid相关的车辆时遇到了问题。它们通过主键和外键连接(参见图片)。

数据库:
如何将一个组合框的结果绑定到另一个组合框中在Visual Studio(C#/SQL)中。

界面:
如何将一个组合框的结果绑定到另一个组合框中在Visual Studio(C#/SQL)中。

我尝试了多种直观的在Visual Studio中设置数据绑定的方式,但它们都显示数据库中的所有车辆,而不仅仅是具有正确ownerID的车辆。

编辑:这是我使用的有效解决方案,感谢Morr1gan的建议。我只需将以下三行代码放入第一个组合框的SelectedIndexChanged事件处理程序中。

int selectedOwnerID;
int.TryParse(ownerComboBox.Text, out selectedOwnerID);
vehicleBindingSource.Filter = $"CarOwnerID = {selectedOwnerID}";
英文:

I am creating an interface to display car owners and their respective cars (think, a pretend DMV).

I have a combobox bound to the database that autopopulates a few text fields (first name, last name,etc), and a second combobox that populates vehicles (make, model, etc) but I am struggling to get the second combobox to display only the cars affiliated with the ownerid in the first. They are connected via primary and foreign key (see images)

Database:
如何将一个组合框的结果绑定到另一个组合框中在Visual Studio(C#/SQL)中。

Interface:
如何将一个组合框的结果绑定到另一个组合框中在Visual Studio(C#/SQL)中。

I have tried several intuitive setups for the data bindings in Visual Studio, but all of them display all cars in the database, not just the ones with the correct ownerID

EDIT: Here is the working solution that i used thanks to Morr1gan, below. I just placed these three lines into the first combobox's SelectedIndexChanged event handler.

int selectedOwnerID;
int.TryParse(ownerComboBox.Text, out selectedOwnerID);
vehicleBindingSource.Filter = $"CarOwnerID = {selectedOwnerID}";

答案1

得分: 2

为了将一个组合框的结果绑定到另一个组合框中,在VS中,你可以使用第一个组合框的SelectedIndexChanged事件来过滤第二个组合框中显示的数据,这取决于第一个组合框中的项目。

对于你的问题,我将使用我自己的命名约定。请随时进行相应的替换,如果出现语法错误,请告诉我。你可以为第二个数据组合框创建一个数据源,其中包括所有的汽车信息,例如在你的情况下的所有者ID。然后,将第二个组合框的DisplayMember属性设置为汽车制造商/型号或您所希望的其他属性。

一旦你完成这些步骤,还可以将第二个组合框的ValueMember属性设置为汽车ID,或者只要它是一个独立的唯一标识符。

使用第一个框的SelectedIndexChanged事件,编写代码以根据第一个组合框中选择的项目过滤第二个组合框中的数据。在MSAccess中,你可以通过将第2个组合框的DataSource属性设置为一个已过滤的数据视图或数据表,该数据视图或数据表将只包括具有适当选定所有者ID的汽车。

示例(原始):

// 假设有两个命名为 ownerComboBox 和 carComboBox 的组合框

// 设置第2个组合框的数据源

carComboBox.DisplayMember = "MakeModelYear";
carComboBox.ValueMember = "CarID";
carComboBox.DataSource = carTable;

// 设置事件处理程序

private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    int selectedOwnerID = (int)ownerComboBox.SelectedValue;

    // 根据选定的所有者ID过滤carComboBox中的数据

    DataView carView = new DataView(carTable);
    carView.RowFilter = $"OwnerID = {selectedOwnerID}";
    carComboBox.DataSource = carView;
}

在上面的示例中,carTable 是一个包含所有汽车数据以及所有者ID的数据表。MakeModelYear字段将汽车的制造商、型号和年份的字符串连接起来,然后将其用作第二个组合框的显示值。

ownerComboBox_SelectedIndexChanged 是一个事件处理程序,根据所有者ID过滤来自carTable的数据,然后将另一个组合框(第2个)的DataSource属性设置为一个DataView,该DataView只包括那些经过筛选的汽车数据。它还会根据你在第一个组合框中选择的项目更新第2个组合框。

英文:

So, to bind the results of one combo box to another in VS, you can use the SelectedIndexChanged event of the first combo box in order to filter the data that is shown in the 2nd box reliant on the item in the first.

For the purpose of your question, I will be using naming conventions of my own. Feel free to replace appropriately and let me know if you receive any syntax errors, please. You can create a data source for your second data combo box that includes all of the car info, such as the owner ID in your case-scenario. Then set a DisplayMember property of the second of your combo boxes (if you have MSAccess, you can do this fairly quickly and easily), property of the second combo box to the car make/model or whichever you desire.

Once you do that, you could also set a ValueMember property of the second combo box to the car ID or as long as it is a separate unique identifier.

Using the SelectedIndexChanged event of the first box, write your code so that it filters the data in the second combo box based on the selected item in your first combo box. You can do this in MSAccess via setting the DataSource property of the 2nd combo box to a filtered data view or data table that only will include the cars with the appropriately selected owner ID.

Example (Raw):

// Assuming 2 boxes named ownerComboBox and carComboBox 

// Setup the data source for the 2nd

carComboBox.DisplayMember = "MakeModelYear";
carComboBox.ValueMember = "CarID";
carComboBox.Datasource = carTable

// Setup the event handler

private void ownerCombobox_SelectedIndexChanged(object, sender EventArgs, e)
{
     int selectOwnerID = (int)ownerComboBox.SelectedValue;

// Filtering out the data in in the carComboBox based on selected owner's ID

DataView carView = new DataView(carTable);
carView.RowFilter = $"OwnerID = {selectedOwnerID}";
carComboBox.DataSource = carView;

In the above example, carTable is a Data Table that will contain all of the car data as well as owner ID. The MakeModelYear field will concatenate the string of the car's make, model, and year, then use it as the display value for the second combo box.

The ownerComboBox_SelectedIndexChanged is an event handler that filters the data from carTable based on owner's ID then sets the DataSource property of the other combo box (2nd), to a DataView that will only include those filtered cars. Will also update the 2nd box as well based on your selected item in the first combo box.

huangapple
  • 本文由 发表于 2023年2月27日 12:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定