0
votes

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();
 }       

Thank you but I have all that already and I’m getting all nodes to grid. The problem I’m having is limiting to checked child nodes from filNode.Nodes. I’ve been playing with ‘if (fileNode.Checked)’ but is never true even though I can print it in immediate window

?fileNode.Nodes[2]
{Text = "1  - CARD COLOR"}
    base: {Text = "1  - CARD COLOR"}
    BackColor: "{Name=0, ARGB=(0, 0, 0, 0)}"
    Bounds: {X = 76 Y = 176 Width = 92 Height = 16}
    Checked: true





  private DataTable getFieldsTable()
    {
        //original
        DataTable dt = new DataTable();
        dt.Columns.Add("ColumnName");
        dt.Columns.Add("FMFieldName");
.
.
.
        dt.Columns.Add("PointsToFileNumber");
        TreeNode fileNode = tvFileMan.SelectedNode;
        foreach (TreeNode tn in fileNode.Nodes)
        {
            if (tn.Nodes.Count == 0)
            {   
                if (fileNode.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 TreeNode
                if (dr["PointsToFileNumber"].ToString().Length > 0)
                {
                    dr["ColumnName"] = suggestName(tn.Text.Substring(tn.Text.IndexOf("  - ") + 4) + "txt");
                }
                dt.Rows.Add(dr);
                }
            }
        }
        return dt;

    }
2

2 Answers

0
votes

Make the Datatable a private member variable and add records to it when Parent Nodes are checked:

//The DataTable is now a private member variable and is accessible in the tvFileMan_AfterCheck event 
private DataTable dt = getFieldsTable();

private void getChildNodesToGrid()
   {
        dgvColumns.DataSource = dt; //this binds the DataTable to the GridView
        getAttributeSIDs();
    }

private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
    {
        getFileAndColumns();
        if (e.Node.Nodes.Count > 0)
        {
           this.CheckAllChildNodes(e.Node, e.Node.Checked);
            e.Node.Expand();
            foreach (TreeNode tn in e.Node.Nodes)
            {
                if (tn.Nodes.Count.Equals(0))
                    tn.Checked = e.Node.Checked;
            }

            //HERE Add the treenode(s) to the DataTable
            DataRow dr = dt.NewRow();
            dr[0] = "e.Node.Text";
            dt.Rows.Add(dr);

            getChildNodesToGrid();
 }       
0
votes

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)'. Again I 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++;
            }
        }
        return dt;

    }
   private void getFileAndColumns()
    {
        label4.Visible = false;
        label5.Visible = false;
        btAllFields.Visible = false;
        cbComputed.Checked = false;
        TreeNode node = tvFileMan.SelectedNode;
        if (node == null) return;
        // Is it a File or Field?
        if (node.Index == 0) return;

        if (node.Nodes.Count > 0)
        {
            // FileManFile selected.
            // Check AttributeMap for this file. If found, fill in textboxes with that info.

            // Otherwise, suggest a name for the table.
            tbFileNumber.Text = node.Tag.ToString();
            tbFileName.Text = node.Text.Substring(node.Text.IndexOf("  - ") + 4);
            tbTableName.Text = "xxxx." + (suggestName(tbFileName.Text) + "F" + tbFileNumber.Text.Replace('.', 'x'));
            label4.Text = "To select all fields push button to the right. \nOtherwise double-click fields to add them one at a time.";
            label4.Visible = true;
            btAllFields.Visible = true;
            dgvColumns.DataSource = null;
            dgvPKIENS.DataSource = getPKIENSTable();
            getPKIENSIDs();
        }
        else
        {
            // FileMan Field selected.
        }


    }