How to Bind DataTable into Treeview
How to Bind Datatable into Treeview using C#,
I am using visual c# express, and there was a case, I have a table like this:
I Only have 2 Column, that is ParentID and CustChannelID
ParentID Column is a parent from CustChannelID Column
and What I wanted is create a treeview like this one
and this is my method
private void frmDODaily_Load(object sender, EventArgs e)
{
//I defined the function of Initialization Tree
treeV();
}
private void treeV()
{
//Get Class to get database
csDailyDO dodaily = new csDailyDO();
//Define the tree
TreeNode tree = new TreeNode();
//Get Datatable, you should know how to get one from your database
DataTable data = dodaily.ChannelTree(“MTL_”);
//Initial for first tree
tree = createnode(tree, data, “LOCAL”);
tree.Text = “LOCAL”;
treeView1.CheckBoxes = true;
treeView1.Nodes.Add(tree);
}
//and this is my method, please shared to anyone
private TreeNode createnode(TreeNode parent, DataTable data, string LatestKey)
{
TreeNode result = new TreeNode();
TreeNode temp = new TreeNode();
foreach (DataRow row in data.Rows)
{
if (row[“ParentID”].ToString() == LatestKey)
{
temp = createnode(parent, data, row[“CUSTCHANNELID”].ToString());
temp.Text = row[“CUSTCHANNELID”].ToString();
result.Nodes.Add(temp);
}
}
return result;
}
Please Comment and shares this codes
Categories: Computer Programming, dynamics ax 2009
c#, Computer Programming
Comments (0)
Trackbacks (0)
Leave a comment
Trackback
Recent Comments