通过选中树视图的子节点复选框,将行添加到datagridview
本文关键字:添加 datagridview 复选框 子节点 视图 | 更新日期: 2023-09-27 18:21:21
我是C#的新手,正在进行第一个项目。
我有一个WINFORM,它显示一个树节点和一个数据网格视图。我希望从树节点中选择的项目转到数据网格视图。如果我选择treenode的父节点,所有子节点都会进入datagridview,但如果我只是展开父节点并选择其中一个子节点,则不会进入datagrid。(树节点的选择激发了一种从sql表中获取项目属性的方法,它是进入网格+节点值的属性)
当选择父级时,"if(e.Node.Nodes.Count>0)"为true。如果未选择父项,但子项为"if(e.Node.Nodes.Count>0)",则为false。
所以我的问题是,我需要什么代码来查找已检查/未检查的子节点?
一旦我得到了正确的代码来查找子节点检查,如果子节点未检查,什么代码会将其移动到数据网格或从数据网格中删除?
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();
}
谢谢,但我已经有了所有这些,我正在将所有节点放到网格中。我遇到的问题是限制从filNode.nodes检查的子节点。我一直在玩"if(fileNode.checked)",但从来都不是真的,即使我可以在即时窗口中打印它
?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;
}
使Datatable成为私有成员变量,并在检查父节点时向其添加记录:
//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();
}
在AfterCheck中添加了一个'else',因此它只需调用getChildNodesToGrid()
然后,我更新了私有DataTable getFieldsTable(),添加了一个计数器和条件"if(fileNode.Nodes[cnt].Checked)"。同样,我不知道这是否是正确的编程,但似乎有效。
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.
}
}