Problem to update hierarchical data and display it in treeView.
-
Hi i have a simple app which display emplyees organization chart from northwind database. here is my code to accomplish this :
private void btnLoadData_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Clear();this.\_dtEmployees = this.GetEmployees(); this.FillHieraricalData(this.\_dtEmployees, this.treeView1, "LastName", "EmployeeID", "ReportsTo"); this.treeView1.ExpandAll();
}
private DataTable GetEmployees()
{
DataTable dt = new DataTable();using (SqlConnection con = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString)) { SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "select \* from employees"; con.Open(); dt.Load(cmd.ExecuteReader()); con.Close(); } return dt;
}
private void FillHieraricalData(DataTable dt, TreeView treeView, string infoColumnName,
string pkColumnName, string parentColumnName)
{
TreeNode node = null;
foreach (DataRow row in dt.Rows)
{
node = new TreeNode(row[infoColumnName].ToString());
node.Tag = row[pkColumnName];if (row\[parentColumnName\] != DBNull.Value) { List<TreeNode> foundNodes = new List<TreeNode>(); // search over treeView nodes to found parent of row (if exists) SearchNodeInTreeView(treeView.Nodes, Convert.ToInt32(row\[parentColumnName\]), foundNodes); if (foundNodes.Count > 0) foundNodes\[0\].Nodes.Add(node); } else // if row parent does not exists, then it's parent itself (or it's root), it should add to level 0 of treeView treeView.Nodes.Add(node); }
}
// this method search in treeView.Nodes to find specific nodes base on 'value'
private void SearchNodeInTreeView(TreeNodeCollection nodesToSearch, int value, List<TreeNode> foundNodes)
{
for (int i = 0; i < nodesToSearch.Count; i++)
{
if (Convert.ToInt32(nodesToSearch[i].Tag) == value)
foundNodes.Add(nodesToSearch[i]);//Recursively search in the child nodes SearchNodeInTreeView(nodesToSearch\[i\].Nodes, value, foundNodes); }
}
However, when i update data in memory (via dataTable) and redisplay data in treeView, the row that has been modfied has been removed from treeView and not display. for example, when i change 'Reports To' of any employee, that employee does not display in treeView! here
-
Hi i have a simple app which display emplyees organization chart from northwind database. here is my code to accomplish this :
private void btnLoadData_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Clear();this.\_dtEmployees = this.GetEmployees(); this.FillHieraricalData(this.\_dtEmployees, this.treeView1, "LastName", "EmployeeID", "ReportsTo"); this.treeView1.ExpandAll();
}
private DataTable GetEmployees()
{
DataTable dt = new DataTable();using (SqlConnection con = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString)) { SqlCommand cmd = con.CreateCommand(); cmd.CommandText = "select \* from employees"; con.Open(); dt.Load(cmd.ExecuteReader()); con.Close(); } return dt;
}
private void FillHieraricalData(DataTable dt, TreeView treeView, string infoColumnName,
string pkColumnName, string parentColumnName)
{
TreeNode node = null;
foreach (DataRow row in dt.Rows)
{
node = new TreeNode(row[infoColumnName].ToString());
node.Tag = row[pkColumnName];if (row\[parentColumnName\] != DBNull.Value) { List<TreeNode> foundNodes = new List<TreeNode>(); // search over treeView nodes to found parent of row (if exists) SearchNodeInTreeView(treeView.Nodes, Convert.ToInt32(row\[parentColumnName\]), foundNodes); if (foundNodes.Count > 0) foundNodes\[0\].Nodes.Add(node); } else // if row parent does not exists, then it's parent itself (or it's root), it should add to level 0 of treeView treeView.Nodes.Add(node); }
}
// this method search in treeView.Nodes to find specific nodes base on 'value'
private void SearchNodeInTreeView(TreeNodeCollection nodesToSearch, int value, List<TreeNode> foundNodes)
{
for (int i = 0; i < nodesToSearch.Count; i++)
{
if (Convert.ToInt32(nodesToSearch[i].Tag) == value)
foundNodes.Add(nodesToSearch[i]);//Recursively search in the child nodes SearchNodeInTreeView(nodesToSearch\[i\].Nodes, value, foundNodes); }
}
However, when i update data in memory (via dataTable) and redisplay data in treeView, the row that has been modfied has been removed from treeView and not display. for example, when i change 'Reports To' of any employee, that employee does not display in treeView! here
You are putting the row id into the tag property
node.Tag = row[pkColumnName];
But I don't see where you find that row to update the record when editing. You may be updating row[0] all the time. I have been known to stick the entire row object into the tag property.
node.Tag = row;
Never underestimate the power of human stupidity RAH
-
You are putting the row id into the tag property
node.Tag = row[pkColumnName];
But I don't see where you find that row to update the record when editing. You may be updating row[0] all the time. I have been known to stick the entire row object into the tag property.
node.Tag = row;
Never underestimate the power of human stupidity RAH