如何使用treeview过滤datagridview [英] How to use treeview to filter datagridview
本文介绍了如何使用treeview过滤datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用treeviews来过滤datagridview。这意味着要动态过滤。
到目前为止我有什么:
1.带有记录和三个树视图工具的数据网格视图[treeview1 ,treeview2&treeview3]
2.过滤datagridview记录的三种方法[我的代码]
3.点击事件后我的三个树视图[我的代码]
期望:想要像excel一样动态过滤:
I'm trying to use treeviews to filter datagridview.That means to filter dynamically.
What I have so far:
1. A datagridview with records and three treeview tool [treeview1, treeview2 & treeview3]
2. Three methods which filters the datagridview records [my code]
3. After click event for my three treeview [my code]
Expectation: Want to dynamic filter like excel:
推荐答案
我写了一个通用的rowFilter方法,后来在DataView级别应用了rowFilter方法。 />
感谢Maciej Los的帮助:
I wrote a generic rowFilter methode and later applied the rowFilter method at the DataView level.
Thanks to Maciej Los for the help:
// The method applies to all treeviews
foreach(TreeNode node in treeView1.Nodes) // Root treeview
{
if(node.Nodes.Count > 0)
{
// Iterate through the children nodes to start building the rowFilter string
foreach(TreeNode childNode in node.Nodes)
{
if (childNode.Checked)
{
if(treeview1.Length > 0)
{
treeview1RowFilter += " OR ";
}
treeview1RowFilter += "[AreaCode] = " + childNode.Text;
}
else if(childNode.Checked)
{
MessageBox.Show("Combobox is empty");
}
}
}
}
// The same foreach loop for two remaining treeview
......
// rowFilter strings that will have to concantenate
string rowFilter = string.Empty;
if (cb11.Checked)
{
rowFilter += " [AreaCode] = " + cb11.Text.Trim();
}
if (cb16.Checked)
{
if (rowFilter.Length > 0)
rowFilter += " OR";
rowFilter += " [AreaCode] = " + cb16.Text.Trim();
}
if (cb31.Checked)
{
if (rowFilter.Length > 0)
rowFilter += " OR";
rowFilter += " [AreaCode] = " + cb31.Text.Trim();
}
rowFilter = "(Grant='Yes') AND (AreaCode = 11 OR AreaCode = 31) AND (Land='North' OR Land='Schweiz')";
try
{
//Check an see what's in the dgv
DataView dv = new DataView(dt);
dv.RowFilter = rowFilter;
datagridview.Datasource = dv;
}
catch (Exception)
{
MessageBox.Show("Can’t find the column", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
调试程序并检查rowFilter
返回的内容。似乎你错过了''
围绕childnote.Text
返回的文本。它应该类似于FieldName ='Some Text'
有关详细信息,请参阅:
DataView.RowFilter属性 [ ^ ]
DataView RowFilter语法[C#] [ ^ ]
在这个论坛上有几次提到字符串是不可变的。该怎么办?请阅读:为什么字符串是不可变的,什么是它的含义是什么? [ ^ ]
< a href =https://msdn.microsoft.com/en-us/library/system.data.dataview%28v=vs.110%29.aspx> DataView类 [ ^ ]启用过滤,但必须满足一些条件,请参阅: DataColumn.Expression [ ^ ]和排序和过滤数据 [ ^ ]
因此,根据您在评论中发布的图片,是
或否
值用于Grant
列,然后
Debug your program and check whatrowFilter
returns. Seems you missed''
around the text returned bychildnote.Text
. It should be something like"FieldName = 'Some Text'"
For further information, please see:
DataView.RowFilter Property [^]
DataView RowFilter Syntax [C#][^]
Several time on this forum was mentioned that string is immutable. What to do? Please, read this: Why strings are immutable and what are the implications of it?[^]
DataView class[^] enables filtering, but some criteria must be fulfilled, see: DataColumn.Expression[^] and Sorting and Filtering Data[^]
So, based on the images you posted in comment,Yes
orNo
value is used forGrant
column, then
rowFilter = "Grant='Yes'";
如果你想添加几个条件,请使用:
If you want to add several condtions, use:
rowFilter = "(Grant='Yes') AND (AreaCode = 11 OR AreaCode = 31) AND (Land='North' OR Land='Schweiz')";
试试!
Try!
这篇关于如何使用treeview过滤datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文