2013-11-22 53 views
-1

这是我正在尝试做的事情。我有一个表单,带有一个“过滤器”组合框和一个DataGridView,用于显示数据库中的数据(我已经使用DataSet.Tables[0].DefaultView属性分配了数据,组合框包含一个项目列表,在数据库中找到,还有一个自定义。增加了一个(命名)当用户选择的项目之一,我下面的代码运行:刷新DataView的RowFilter属性

 int filterID = (int)this.cbxFilter.SelectedValue; 
     DataView view = this.dgvScenarios.DataSource as DataView; 

     if (filterID > 0) { 
      view.RowFilter = string.Format("Season = {0}", this.cbxFilter.SelectedValue); 
     } else { 
      view.RowFilter = string.Empty; 
     } 

现在,这工作得非常好,因为<All>项为第0。现在,我可以改变'filtering'property,Season,在窗体上,它有一个类似的组合框,其中包含与“过滤器”框相同的所有数据,减去<All>项,当用户更改此值时,这就是运行的内容:

 if (this._loading) { 
      return; 
     } 

     ComboBox cbx = sender as ComboBox; 
     int rows = this.UpdateDataFromControl(cbx.Tag, cbx.SelectedValue); 

     if (rows <= 0) { 
      return; 
     } 

     this.UpdateDGVCell(cbx.Tag, cbx.SelectedValue, "ID"); 
     this.UpdateDGVCell(cbx.Tag, cbx.Text, "Text"); 

现在,我会认为DataView会更新数据网格,但它什么都不做,我不知道如何在不将数据再次加载到数据集的情况下进行刷新,这将成为性能,因为我正在访问数据库,再次,然后过滤出来。我是否错过了Google上尚未找到的内容?感谢您的帮助,如果需要更多信息,请告诉我!

+0

“刷新RowFilter”是什么意思? –

+0

当我更改DataSet中的'Season'列时,我期待DataView更新并从DataGridView中删除它,因为它不再是有效的ID。然而,这并不是什么情况。 DataGridView已更新以显示新的“季节”值,但未删除。 –

+0

要重置'RowFilter',只需将其设置为'string.Empty',看起来好像是在代码中的某处完成的。 –

回答

0

是否有原因使用表的DataView,而不是在BindingSource中使用表?我发现BindingSource易于使用。

某处在你的代码,你可能有这样的事情:

dataGridView.DataSource = yourDataSource; 

如果替换成

this.bindingSource = new BindingSource(); 
this.bindingSource.DataSource = yourDataSource; 
dataGridView.DataSource = this.bindingSource; 

那么你可以申请过滤(与自动触发更新的DataGridView显示事件)如下:

this.bindingSource.Filter = "Season = 'x'"; //automatically updates the dataGridView 

要删除过滤器,您可以使用

this.bindingSource.RemoveFilter(); //automatically updates the dataGridView 

这是否解决您所要做的事情?