How to Show Data In TreeView

It you have some data in table and want to show it in TreeView
You can write code below:
1. tbl_Data have have data below:
2. Then please write code below:
TreeView1.Nodes.Clear();
private void showtreeview()
SqlCommand cmd= new SqlCommand ();
cmd.Connection =conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText = "Select * from tbl_data order by Leve";
SqlDataReader sqlDR = cmd.ExecuteReader();

TreeView1.Nodes.Clear();

int lvCode = 0;

while (sqlDR.Read())
{
TreeNode tn = new TreeNode();
tn.Text = sqlDR["Level"].ToString() + "-" + sqlDR["Text"].ToString();
tn.Value = sqlDR["Level"].ToString();

if (sqlDR["Level"].ToString().Equals("1")){

TreeView1.Nodes.Add(tn);
TreeView1.Nodes[TreeView1.Nodes.Count - 1].Select();

}else
{
if ( lvCode 0)
{
TreeView1.SelectedNode.ChildNodes "+
" [TreeView1.SelectedNode.ChildNodes.Count-1].Select();
}
}
else if (lvCode > int.Parse(sqlDR["Level"].ToString()))
{
TreeView1.SelectedNode.Parent.Select();
}


TreeView1.SelectedNode.ChildNodes.Add(tn);
}
lvCode = int.Parse(sqlDR["Level"].ToString());


}
}

3. When already to finish your code you can run it.

No comments:

Post a Comment