add row to datagridview by checking child node check box of treeview
-
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();
} -
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();
}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++; } }