title: How to filter datagridview using treeview checkboxes
body: <p>I've a <code>datagridview</code> which gets its data from a database and <code>treeview checkBoxes</code> which should be used as a filter.</p>
  
  <p>I want to filter the <code>datagridview</code> by checking / unchecking one or more of the <code>treeview checkedboxes</code> and should display me the selected item in the <code>datagridview</code> with it relating Amounts:</p>
  
  <p><img src="https://i.stack.imgur.com/oub6x.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/Kjf6S.png" alt="enter image description here"></p>
  
  <p>My code I' trying to convert from <code>checkedboxes</code> into <code>treeview checkboxes</code>:</p>
  
  <pre><code>private void treeview1()
  {
  
      string rowFilter = string.Empty;
  
      foreach (TreeNode node in treeView1.Nodes)
      {
    // root nodes to filter on,to check children nodes under each root node
          if (node.Text == "AreaCode")
          {
    // Iterate through the root node's children nodes and build filter based      on them being checked or unchecked
              foreach (TreeNode childNode in node.Nodes)
              {
                  if (childNode.Checked)
                  {
                      if (rowFilter.Length &gt; 0)
                      {
                          rowFilter += " OR ";
                      }
                      rowFilter += "[AreaCode] = " + childNode.Text;
                      MessageBox.Show(rowFilter);
                  }
              }
          }
   }
  
  
  
  
      // Apply rowFilter to DataView.RowFilter 
  
      try{
          //Check an see what's in the dgv
          DataView dv = new DataView(dt);
          dv.RowFilter = rowFilter;
          datagridview1.DataSource = dv;
          }
      catch(Exception)
          {
              MessageBox.Show("IT IS NOT WORKING");
          }
  
  
  }
  
  
   private void treeview2()
  {
  
      string rowFilter = string.Empty;
  
      foreach (TreeNode node in treeView1.Nodes)
      {
          // In case you add more root nodes to filter on, you'll want to check children nodes under each root node
          if (node.Text == "Grant")
          {
              // Iterate through the root node's children nodes and build your filter based on them being checked or unchecked
              foreach (TreeNode childNode in node.Nodes)
              {
                  if (childNode.Checked)
                  {
                      if (rowFilter.Length &gt; 0)
                      {
                          rowFilter += " OR ";
                      }
  
       // Outcome is a string NOT WORKING RIGHT                   
    rowFilter += "[Grant] = " + childNode.Text; // gets the selected string but   can filter If I put = 'Yes' ist makes it static. I need to make it dynamic
                      MessageBox.Show(rowFilter);
                  }
              }
          }
    }
  
  
  
  
      // Apply rowFilter to DataView.RowFilter 
  
      try{
          //Check an see what's in the dgv
          DataView dv = new DataView(dt);
          dv.RowFilter = rowFilter;
          datagridview1.DataSource = dv;
          }
      catch(Exception)
          {
              MessageBox.Show("IT IS NOT WORKING");
          }
  
  
  }
  
   private void treeview3()
  {
  
      string rowFilter = string.Empty;
  
      foreach (TreeNode node in treeView1.Nodes)
      {
          // In case you add more root nodes to filter on, you'll want to check children nodes under each root node
          if (node.Text == "Land")
          {
              // Iterate through the root node's children nodes and build your filter based on them being checked or unchecked
              foreach (TreeNode childNode in node.Nodes)
              {
                  if (childNode.Checked)
                  {
                      if (rowFilter.Length &gt; 0)
                      {
                          rowFilter += " OR ";
                      }
  
      // Outcome is a string NOT WORKING RIGHT                   
      rowFilter += "[Land] = " + childNode.Text; // gets the selected string but can filter If I put = 'Mitte' ist makes it static. I need to make it dynamic
                      MessageBox.Show(rowFilter);
                  }
              }
          }
      }
  
  
  
  
      // Apply rowFilter to DataView.RowFilter 
  
      try{
          //Check an see what's in the dgv
          DataView dv = new DataView(dt);
          dv.RowFilter = rowFilter;
          datagridview1.DataSource = dv;
          }
      catch(Exception)
          {
              MessageBox.Show("IT IS NOT WORKING");
          }
  
  
  }
  
  
  
  
    private void treeViewAfterCheck_AfterCheck(object sender, TreeViewEventArgs e)
      {
          // Any node that is checked/unchecked will have all of its 
          // children nodes checked/unchecked
          if (e.Node.Nodes.Count &gt; 0)
          {
              foreach (TreeNode childNode in e.Node.Nodes)
              {
                  childNode.Checked = e.Node.Checked;
              }
          }
  
     treeview1();
     treeview2();
      treeview3();
  
        }
  </code></pre>