Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Posted
Updated 18-Apr-15 11:21am
v4

I wrote a generic rowFilter methode and later applied the rowFilter method at the DataView level.

Thanks to Maciej Los for the help:

C#
// 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);
        }
 
Share this answer
 
v3
Comments
Maciej Los 18-Apr-15 17:38pm    
The best 'Thank you' is whenever you accept valuable answer as a solution ;)
BTW: your answer does explain nothing.
[EDIT]
You have to provide "generic method" you implemented.
mikybrain1 19-Apr-15 6:59am    
hahahaha. I did implemented 'ur suggestion and played around it till it worked. Thnx.
Maciej Los 19-Apr-15 7:02am    
Thank you, Mike. What i'm trying to tell is that the answer must be more detailed, especially when you say you use "generic method"...
mikybrain1 19-Apr-15 9:01am    
Have updated my solution.
Maciej Los 19-Apr-15 9:03am    
+5 for effort
Debug your program and check what rowFilter returns. Seems you missed '' around the text returned by childnote.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?[^]

[EDIT]
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 or No value is used for Grant column, then
C#
rowFilter = "Grant='Yes'";


If you want to add several condtions, use:
C#
rowFilter = "(Grant='Yes') AND (AreaCode = 11 OR AreaCode = 31) AND (Land='North' OR Land='Schweiz')";


Try!
 
Share this answer
 
v2
Comments
mikybrain1 16-Apr-15 15:50pm    
As I wrote the rowFilter returns Yes or No depending on what I clicked BUT dv.RowFilter isn't receiving it
mikybrain1 16-Apr-15 16:29pm    
If I change the text name of the treeview child node from string (Yes or No) to number,
it works fine and filters my datagridview. And I don't understand why
Maciej Los 16-Apr-15 16:35pm    
As i mentioned, yes and no is string, so you need to wrap it into ''!
mikybrain1 16-Apr-15 16:57pm    
I really understand u Maciej and u always help me But what if I've 8 options and I wanna click and filter 4. How would I Iterate through them. I wanna archieve something like Excel
Maciej Los 16-Apr-15 17:20pm    
Miky, i don't get you... ;( Could you be so kind and explain it bit more...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900