过滤DataGridView中的行? [英] Filter rows in DataGridView?

查看:70
本文介绍了过滤DataGridView中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像是一个新手的问​​题,但它实际上并不是。



问题是我只想要我的DataGridView'的DataSource这样:



It looks like a newbie''s question but it''s not actually.

The problem is I just want my DataGridView''s DataSource like this:

myDataGridView.DataSource = myDataSet;
myDataGridView.DataMember = "myTableName";
//however filtering its rows like this doesn't work:
myDataSet.Tables["myTableName"].DefaultView.RowFilter = "...";





Plus,myDataSet已经绑定到我的表单上的一些控件,我不想重新绑定它,以防myDataGridView的DataSource发生变化。



如果这个条件不是在这种情况下,我会使用以下内容并可以过滤行OK:





Plus, myDataSet is already bound to some controls on my form and I don''t want to rebind it in case that myDataGridView''s DataSource changes.

If that condition is not the case, I would use the following and can filter rows OK:

//choice 1
myDataGridView.DataSource = myDataTable;
//then filter rows like this:
myDataTable.DefaultView.RowFilter = "...";

//choice 2
BindingSource bs = new BindingSource(myDataSet, "myTableName");
myDataGridView.DataSource = bs;
//then filter rows like this:
bs.Filter = "...";





我只想在第一个代码片段中使用它。在这种情况下如何过滤行?



提前感谢您的帮助!



I just want it as in the first code snippet. How can I filter its rows in that case?

Thank you in advance for your help!

推荐答案

你不能在没有BindingSource的情况下过滤行,除非你重新加载数据源并改变它包含的实际行 - 这就是你首先使用绑定源的原因:充当可以的中介b>被过滤。
You can''t filter rows without a BindingSource unless you reload the data source and change the actual rows it contains - that is why you use a Binding Source in the first place: to act as an intermediary which can be filtered.


这篇关于过滤DataGridView中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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