C#WinForms - 基于数据绑定datagridview中另一个组合框的值过滤一个组合框

C#WinForms - 基于数据绑定datagridview中另一个组合框的值过滤一个组合框

问题描述:

我有4个表 - 代理,客户,县和城镇。代理商和客户都有一个镇域和一个县域。我有一个DataGridView每个表。这些工作很好。我使用城镇县作为数据源的镇和县作为组合框。C#WinForms - 基于数据绑定datagridview中另一个组合框的值过滤一个组合框

问题是它不会根据选定的县过滤城镇。我希望这样做,但没有选项可以根据另一个字段的值过滤组合框。

我已经搜索了一段时间,但无法找到有用的东西。

任何人都可以通过如何做到这一点告诉我吗?

在此先感谢。

问候,

理查德

PS我使用Visual Studio 2010中,大多设计视图。

+0

您是否在使用像SqlDataSource或ObjectDataSource这样的DataSource对象来获取县?如果你这样做,那么你应该通过一个选择参数来过滤县,具体取决于Town DropDown中选择的值。 – 2010-10-30 11:00:43

+0

我正在使用一个DataSet和一个mdf文件。我将表拖到DataSet上,然后我使用那里显示的表来可视化DataGridViews中的数据。对不起,如果我的措辞不正确 - 我只在2天前开始使用C#。 – ClarkeyBoy 2010-10-30 11:02:29

+0

@ClarkeyBoy,提供相关代码。 – mahesh 2010-10-30 11:16:09

你的数据是如何绑定的?如果您使用DataView,则可以指定RowFilter属性,然后刷新基础数据。 rowfilter属性像where子句一样工作,只返回实际数据的一个子集。

A little background on DataView

为了能够做到这一点,你应该有一个国家外键字段在你城镇表。

如果你有它,问题可能在于你的Towns组合框是如何数据绑定的,即选择Datasource属性。你不应该把它直接绑定到城镇表,而是绑定到国家表的城镇“外键”。我认为你可以在设计视图中做到这一点。

+0

认为这可能是它。它刚开始的时候就是文字,但从那以后,我取得了很多进展,现在增加了诸如镇/县查找之类的东西,而不是像一个客户的Blandford,而是另一个客户的Blandford论坛。这样,每个城镇都会有一个名称,因此搜索结果会更准确。 – ClarkeyBoy 2010-10-30 11:33:36

可以使用DataView为您的组合框数据源,因为这可以让你筛选基于一个标准(通过RowFilter属性)行。我将展示一个简单的例子,涉及用于选择该国家和乡镇的两个组合框。


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

// 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 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; 

最后,无论何时在国家组合框中选择另一个国家/地区,请更新行过滤器ER:

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

我相信你可以使用数据绑定和一个自定义Format事件处理程序自动完成这最后一步,但我不会详谈。

+0

这看起来是正确的答案(或者在任何情况下都是正确答案)。我正在研究它,如果我以这种方式解决问题,它将标记为答案。 – ClarkeyBoy 2010-10-30 12:05:46

+0

我在事件SelectedIndexSelected处收到'System.NullReferenceException'。目前,我不知道为什么,但在交换'countryComboBox.DataSource = countriesTable;'行后用'countryComboBox.ValueMember =“CountryID”;'它的工作正常。 – 2015-01-27 16:55:10

+0

@DanielBonetti:谢谢你的提示。我修复了我的代码。额外的初始'DataSource = null'确保新的'DisplayMember'和'ValueMember'不会被应用到一些以前设置的数据源。由于性能方面的考虑,''DisplayMember'和'ValueMember'应该总是设置'DataSource' *。 – stakx 2015-01-27 17:18:06

这是我的新答案。事实证明我看错了这个问题(抱歉)。在我的示例中,我正在使用OleDb连接到Access数据库。这是我正在使用的应用程序的代码片段(组合框和表格的名称已被更改为示例)。它所做的只是在comboBox1上更改选择时查询数据库并将结果添加到comboBox2。

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      //Open connection to database... 
      connection.Open(); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connection; 
      string query1 = "select * from Your_Table where Title='" + comboBox1.Text + "'"; 
      command.CommandText = query1; 

      OleDbDataReader reader1 = command.ExecuteReader(); 
      while (reader1.Read()) 
      { 
       comboBox2.Items.Add(reader1["ColumnName"].ToString()); 
      } 
      connection.Close(); 
     } 
     catch (System.Exception ex) 
     { 
      MessageBox.Show("Error " + ex); 
     } 
    } 

这将基于comboBox1查询您的数据库,并根据您的选择将结果放在comboBox2中。

希望这会有所帮助!

+0

该问题被要求过滤组合框,而不是datagridview。 BindingSource不能过滤ComboBox。 – 2016-12-24 05:28:33

+0

我的确读过这个问题了。希望在你给我负面评价之前,我会抓住那个......谢谢你! – 2017-01-10 20:52:36