在我的应用程序中,我需要带有子节点的treeview结构,appilcation应该是这样的,我可以拥有多个没有节点的子节点。当用户点击这些节点时,它应该是可扩展的,我怎么能这样做
答案 0 :(得分:0)
如果我理解你的意思,你可以添加一个额外的节点作为标题,例如:
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
string Value = string.Empty;
string header = string.Empty;
// this is just extra node for header
if (dtParent.Columns.Count > 2)
{
header += "Name" + " " + " ";
header += "VehicleType" + " " + " ";
header += "Capacity" + " " + " ";
}
else
{
header += "Name" + " " + " ";
}
TreeNode headerNode = new TreeNode
{
Text = header.ToString(),
// Any value that you want and has no confelict with other
Value = row["Id"].ToString()
};
if (dtParent.Columns.Count > 2)
{
Value += row["Name"].ToString() + " " + " ";
Value += row["VehicleType"].ToString() + " " + " ";
Value += row["Capacity"].ToString() + " " + " ";
}
else
{
Value += row["Name"].ToString() + " " + " ";
}
TreeNode child = new TreeNode
{
Text = Value.ToString(),
Value = row["Id"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT Id, Name,VehicleType,Capacity FROM Bikes WHERE VehicleTypeId = " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
或者如果你的意思是找到每个列名,你可以使用它:
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
string Value = string.Empty;
string header = string.Empty;
// Get all columns name
for (var i=0;i<(dtParent.Columns.Count)-1;i++)
{
header += dtParent.Columns[i].ColumnName + " ";
}
TreeNode headerNode = new TreeNode
{
Text = header.ToString(),
// Any value that you want and has no confelict with other
Value = row["Id"].ToString()
};
if (dtParent.Columns.Count > 2)
{
Value += row["Name"].ToString() + " " + " ";
Value += row["VehicleType"].ToString() + " " + " ";
Value += row["Capacity"].ToString() + " " + " ";
}
else
{
Value += row["Name"].ToString() + " " + " ";
}
TreeNode child = new TreeNode
{
Text = Value.ToString(),
Value = row["Id"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT Id, Name,VehicleType,Capacity FROM Bikes WHERE VehicleTypeId = " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}