Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. add row to datagridview by checking child node check box of treeview

add row to datagridview by checking child node check box of treeview

Scheduled Pinned Locked Moved C#
questioncsharpcssdatabase
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11025596
    wrote on last edited by
    #1

    I'm new to C# and am working on first project. I have a WINFORM that displays a treenode and a datagridview. I want selected items from the treenode to go to the datagridview. If I select the parent node of treenode all children go to datagridview but if I just expand the parent and select one of the children, nothing goes to datagrid. (selection of a treenode fires a method to get attributes of the item from sql table and it is the attributes that go to the grid + node value) When parent is selected 'if (e.Node.Nodes.Count > 0)' is true. When parent isn't selected but child is 'if (e.Node.Nodes.Count > 0)' is false. So my question is what code do I need to find child nodes that are checked/unchecked? Once I get correct code to find child node check, what code moves it to datagrid or delete from datagrid if child is unchecked?

    private void getChildNodesToGrid()
    {
    // get all child nodes add to dataGridView
    DataTable dt = getFieldsTable();
    dgvColumns.DataSource = dt;
    getAttributeSIDs();
    }

    private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
    {
    getFileAndColumns();
    if (e.Node.Nodes.Count > 0)
    {
    this.CheckAllChildNodes(e.Node, e.Node.Checked);
    // Checked a file so get fields and check all fields except subfiles.
    // Use this event handler to process actions from check box click
    e.Node.Expand();
    foreach (TreeNode tn in e.Node.Nodes)
    {
    if (tn.Nodes.Count.Equals(0))
    tn.Checked = e.Node.Checked;
    }
    getChildNodesToGrid();
    }

    U 1 Reply Last reply
    0
    • U User 11025596

      I'm new to C# and am working on first project. I have a WINFORM that displays a treenode and a datagridview. I want selected items from the treenode to go to the datagridview. If I select the parent node of treenode all children go to datagridview but if I just expand the parent and select one of the children, nothing goes to datagrid. (selection of a treenode fires a method to get attributes of the item from sql table and it is the attributes that go to the grid + node value) When parent is selected 'if (e.Node.Nodes.Count > 0)' is true. When parent isn't selected but child is 'if (e.Node.Nodes.Count > 0)' is false. So my question is what code do I need to find child nodes that are checked/unchecked? Once I get correct code to find child node check, what code moves it to datagrid or delete from datagrid if child is unchecked?

      private void getChildNodesToGrid()
      {
      // get all child nodes add to dataGridView
      DataTable dt = getFieldsTable();
      dgvColumns.DataSource = dt;
      getAttributeSIDs();
      }

      private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
      {
      getFileAndColumns();
      if (e.Node.Nodes.Count > 0)
      {
      this.CheckAllChildNodes(e.Node, e.Node.Checked);
      // Checked a file so get fields and check all fields except subfiles.
      // Use this event handler to process actions from check box click
      e.Node.Expand();
      foreach (TreeNode tn in e.Node.Nodes)
      {
      if (tn.Nodes.Count.Equals(0))
      tn.Checked = e.Node.Checked;
      }
      getChildNodesToGrid();
      }

      U Offline
      U Offline
      User 11025596
      wrote on last edited by
      #2

      RESOLVED: Added an 'else' to the AfterCheck so it simply calls getChildNodesToGrid() I then updated private DataTable getFieldsTable() to add a counter and condition 'if (fileNode.Nodes[cnt].Checked)'. Don't know if this is proper programming but seems to work.

      private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
      {
      getFileAndColumns();
      if (e.Node.Nodes.Count > 0)
      {
      //this.CheckAllChildNodes(e.Node, e.Node.Checked);
      // Checked a file so get fields and check all fields except subfiles.
      e.Node.Expand();
      foreach (TreeNode tn in e.Node.Nodes)
      {
      if (tn.Nodes.Count.Equals(0))
      tn.Checked = e.Node.Checked;
      }
      getChildNodesToGrid();
      }
      else
      {
      e.Node.Expand();

                  //if (tn.Nodes.Count.Equals(0))
                  if (e.Node.Checked)
                  {
                      //tn.Checked = e.Node.Checked;
                      getChildNodesToGrid();
                  }
      
          }
      

      private DataTable getFieldsTable()
      {
      //original
      DataTable dt = new DataTable();
      dt.Columns.Add("ColumnName");
      dt.Columns.Add("FMFieldName");
      dt.Columns.Add("FMFieldNumber");
      dt.Columns.Add("FMFileNumber");
      dt.Columns.Add("FMFieldType");
      dt.Columns.Add("ResolvedValue");
      dt.Columns.Add("PointsToFileNumber");
      TreeNode fileNode = tvFileMan.SelectedNode;
      int cnt = 0;
      foreach (TreeNode tn in fileNode.Nodes)
      {
      if (tn.Nodes.Count == 0)
      {
      if (fileNode.Nodes[cnt].Checked)
      {
      DataRow dr = dt.NewRow();

                  dr\["FMFieldName"\] = tn.Text.Substring(tn.Text.IndexOf("  - ") + 4);
                  dr\["FMFieldNumber"\] = tn.Tag.ToString();
                  dr\["FMFileNumber"\] = tn.Parent.Tag.ToString();
                  dr\["ColumnName"\] = suggestName(tn.Text.Substring(tn.Text.IndexOf("  - ") + 4));
                  //added by TEA 9/3/14 to get PointsToFileNumber in DataGrid
                  if (dr\["PointsToFileNumber"\].ToString().Length > 0)
                  {
                      dr\["ColumnName"\] = suggestName(tn.Text.Substring(tn.Text.IndexOf("  - ") + 4) + "txt");
                  }
                  dt.Rows.Add(dr);
                  }
                  cnt++;
              }
          }
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups