C# WinForms - 根据数据绑定 datagridview 中另一个组合框的值过滤一个组合框 [英] C# WinForms - filtering one combobox based on the value of another combobox in a databound datagridview

查看:22
本文介绍了C# WinForms - 根据数据绑定 datagridview 中另一个组合框的值过滤一个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 4 个表 - 代理商、客户、县和城镇.代理和客户都有一个城镇字段和一个县字段.我为每个表都有一个 DataGridView.这些工作得很好.我将城镇和县作为组合框,使用城镇和县表作为数据源.

I have 4 tables - Agents, Customers, Counties and Towns. Agents and Customers both have a Town field and a County field. I have a DataGridView for each table. These are working great. I have the Town and County as comboboxes using the Towns and Counties table as the datasource.

问题在于它没有根据所选县过滤城镇.我希望它这样做,但没有选项可以根据另一个字段的值过滤组合框字段.

The problem is that it does not filter the Town based on the selected County. I would like it to do this, but there is no option to filter a combobox field based on the value of another field.

我已经搜索了一段时间,但找不到任何有用的东西.

I have searched this up for a while but cant find anything useful.

谁能告诉我如何做到这一点,好吗?

Can anyone talk me through how to do this, please?

提前致谢.

问候,

理查德

PS 我使用的是 Visual Studio 2010,主要是设计视图.

PS I am using Visual Studio 2010 and mostly design view.

推荐答案

您可以使用 DataView 作为组合框的数据源,因为这允许您过滤行基于标准(通过 RowFilter 属性).我将展示一个简单的示例,其中包含用于选择国家/地区和该国家/地区的城镇的两个组合框.

You can use DataView as a data source for your comboboxes, since this allows you to filter rows based on a criterion (via the RowFilter property). I'll show a simple example involving two comboboxes used for selecting a country and a town in that country.

首先设置一些要使用的数据:

First, set up some data to be used:

// set up DataTable with countries:
countriesTable = new DataTable("Countries");
countriesTable.Columns.Add("CountryID", typeof(int));
countriesTable.Columns.Add("CountryName", typeof(string));
countriesTable.Rows.Add(1, "England");
countriesTable.Rows.Add(2, "Spain");
...

// set up DataTable with towns:
townsTable = new DataTable("Towns");
townsTable.Columns.Add("TownID", typeof(int));
townsTable.Columns.Add("TownName", typeof(string));
townsTable.Columns.Add("CountryID", typeof(int));   // <-- this is a foreign key
townsTable.Rows.Add(1, "London", 1);
townsTable.Rows.Add(2, "Brighton", 1);
townsTable.Rows.Add(3, "Barcelona", 2);
...

<小时>

接下来,将组合框数据绑定到数据:


Next, data-bind the comboboxes to the data:

// bind countries to country combobox:
countryComboBox.DataSource = null;
countryComboBox.DisplayMember = "CountryName";
countryComboBox.ValueMember = "CountryID";
countryComboBox.DataSource = countriesTable;

// bind towns to town combobox:    
townsView = new DataView(townsTable, "CountryID = 1", ...);  // use foreign key
townComboBox.DataSource = null;                              // in a row filter
townComboBox.DisplayMember = "TownName";
townComboBox.ValueMember = "TownID";
townComboBox.DataSource = townsView;

<小时>

最后,每当在国家/地区组合框中选择另一个国家/地区时,更新行过滤器:


Finally, whenever another country is selected in the country combobox, update the row filter:

private void countryComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    ...
    townsView.RowFilter = string.Format("CountryID = {0}",
                                            countryComboBox.SelectedValue);
}

我相信您可以使用数据绑定和自定义 Format 事件处理程序自动执行最后一步,但我不会详细介绍.

I believe you could automate this last step using databinding and a custom Format event handler, but I won't go into details.

这篇关于C# WinForms - 根据数据绑定 datagridview 中另一个组合框的值过滤一个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆