C#的WinForms - 过滤1组合框基于另一个组合框的数据绑定datagridview的价值 [英] C# WinForms - filtering one combobox based on the value of another combobox in a databound 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.
推荐答案
您可以使用数据视图
为您的组合框的数据源,因为这可以让你基于一个标准来过滤行(通过 的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);
...
接下来,数据-bind的组合框的数据:
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);
}
我相信你可以使用数据绑定和一个自定义自动最后一步格式
事件处理程序,但我不会赘述了。
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 - 过滤1组合框基于另一个组合框的数据绑定datagridview的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!