Dev
Dev

Reputation: 1020

Show DataGrid based on the TreeView selection in winform

I have a tree view in my win form which is having more than 10 nodes, node values are read from DB and it changes dynamically, based on selection for each node I have to show the appropriate details (data will be read from DB for the grid) in the DataGrid on right side of the form, is there any simplest way to acheive this?

Upvotes: 0

Views: 1585

Answers (2)

Tim Phan
Tim Phan

Reputation: 311

You can use the property TreeNode.Tag. It is already built to contains data from TreeNode. When loading TreeNode from database, you can load the list data for each node and put it in the Tag property of TreeNode following by code below.

TreeNode treeNode = new TreeNode(textNodeFromDb);
// for exam the LoadListDataByNodeText will return IList<Details>
treeNode.Tag = LoadListDataByNodeText(textNodeFromDb);  

And when user select one node on the TreeView:

if (treeView.SelectedNode != null)
   dataGrid.ItemSource = treeView.SelectedNode.Tag as IList<Details>

To get more information about TreeNode please follow the link below. It already contains the sample code as well:

http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx

Upvotes: 2

Xaqron
Xaqron

Reputation: 30857

DataGrid.ItemsSource = getSelectedNodeDataList(myTreeView.SelectedNode.Text)

getSelectedNodeDataList should return a List of data for that node. If you have duplicate names on TreeView use Index instead of Text.

Upvotes: 0

Related Questions