如何rowFilter datagridview [英] How to rowFilter datagridview
问题描述
我有一个我要过滤的组合框和数据网格视图。在组合框中我有[ Luft ]和[ Bus ]但是在datagridview中我要过滤的列如下所示:
[ ColumnOne ]
AB
AD
AZ
YA
YD
YZ
我的想法是将它们分组这样
[AB,AD,AZ] = Luft和[YA,YD,YZ] =公交车
这意味着如果我在组合框中选择Luft,它应该通过datagridview查看并过滤它:
Combobox ColumnOne
[Luft ] AB
AD
AZ
到目前为止,这是我的rowFilter代码:
if (combobox1.SelectedItem!= null )
{
尝试
{
// 检查看看dgv中的内容
DataVi ew dv = new DataView(dt);
dv.RowFilter = [ColumnOne] LIKE'AB,AD,AZ + combobox1 .Text.Trim()+ %';
datagridview1.DataSource = dv;
}
}
但它不起作用:(
LIKE是一个通配符搜索,类似于文件列表* .txt查找所有具有文本扩展名的文件。所以当你使用
dv.RowFilter = [ColumnOne] LIKE'AB,AD,AZ + combobox1.Text.Trim()+ %';
它查找的行有一个以AB,AD,AZ开头的列和你的组合框内容 - 与其中任何一个都不匹配。
SQL不支持正则表达式比较,所以如果你想把以A开头的字符串与以Z开头的字符串分开,你必须明确它:
string selector = combobox1。 Text .Trim();
dv.RowFilter = (跨度> + selector + ='Luft'AND LEFT([ColumnOne],1)='A')OR( + selector + ='Bus'和LEFT([ColumnOne],1)='Z') 跨度>;
Hi,
I have a combobox and a datagridview which i want to filter. In the combobox i have [Luft]and [Bus] But in the datagridview the column i want to filter looks like this:
[ColumnOne]
AB
AD
AZ
YA
YD
YZ
My thought is to group them like this
[AB,AD,AZ] = Luft and [YA,YD,YZ] = Bus
That means if i select Luft in the combobox, it should look through the datagridview and filter it like this:
Combobox ColumnOne
[Luft] AB
AD
AZ
That's my rowFilter code so far:
if (combobox1.SelectedItem != null)
{
try
{
//Check an see what's in the dgv
DataView dv = new DataView(dt);
dv.RowFilter = "[ColumnOne] LIKE 'AB,AD,AZ" + combobox1.Text.Trim() + "%'";
datagridview1.DataSource = dv;
}
}
BUT it isn't working :(
LIKE is a "wildcard" search, similar to a file list "*.txt" finding all files which have a text extension. So when you use
dv.RowFilter = "[ColumnOne] LIKE 'AB,AD,AZ" + combobox1.Text.Trim() + "%'";It looks for a row that has a column that starts with "AB,AD,AZ" and your combobox content - not match any one of them.
SQL doesn't support regex-style comparisons, so if you want to separate strings starting with "A" from those starting with "Z" you will have to be explicit about it:
string selector = combobox1.Text.Trim(); dv.RowFilter = "(" + selector + " = 'Luft' AND LEFT([ColumnOne],1) = 'A' ) OR (" + selector + " = 'Bus' AND LEFT([ColumnOne],1) = 'Z' )";
这篇关于如何rowFilter datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!