Displaying the nodes of the treeView in the listView

  • Thread starter Thread starter Kam
  • Start date Start date
K

Kam

any example of how to display the nodes of the treeView into the
listView and update it everytime you select other nodes?

a simple code i've created is as follows:
---------------------------------------------------------------------
private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
foreach (DataRow dr in dt.Rows)
{
listView1.Items.Add(dr["Name"].ToString());
}
}

---------------------------------------------------------------------

any help or idea would be very much appreciated.

thank u,
Kam
 
(e-mail address removed) (Kam) wrote in @posting.google.com:
any example of how to display the nodes of the treeView into the
listView and update it everytime you select other nodes?

a simple code i've created is as follows:
---------------------------------------------------------------------
private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
foreach (DataRow dr in dt.Rows)
{
listView1.Items.Add(dr["Name"].ToString());
}
}

---------------------------------------------------------------------

any help or idea would be very much appreciated.

thank u,
Kam

The "TreeViewEventArgs" class contains a property "Node" that returns the
"TreeNode" the event has been fired for. Given that particular tree node
you can get to any other related nodes that you want. "Parent" gets the
node containing the current node. And the "Nodes" property contains all
the child nodes of the current node.

The "Nodes" property returns a "TreeNodeCollection". This will allow you
to loop through all the child nodes of the node in question. That sounds
like what you need. Of coarse for each child node to be helpful you have
to store some information on it that let's you know which record or
object instance it is for. This is why almost every control has a "Tag"
property. The Tag property is for storing any extended information you
want.

I showed you how to use this tag property in your other question
"populate a treeview from a db" on Thursday.

If you want to show just the listview to show exactly the child nodes of
the currently selected node in the listview do somthing like the
following (untested, typed on the fly):

private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
listView1.Items.Clear();
TreeNode tnSel = e.Node;
for (int i = 0; i < tnSel.Nodes.Count; i++)
{
TreeNode tnChild = tnSel.Nodes;
ListViewItem lviChild = new ListViewItem();
lviChild.Text = tnChild.Text;
lviChild.Tag = tnChild.Tag;
listView1.Items.Add(lviChild);
}
}

Now when the user clicks on a listview item you can use the same logic to
find out what record it represents the same way as for the the treeview
(by utilizing the "Tag" property).

I don't see the need to involve your DataSet in this function. your
DataSet was already used in some way to populate the TreeView wasn't it?
 
thank you for your help. I've found out a 2 lines of code that does
the whole thing:

//------------------------------------------------
listView1.Items.Clear();
foreach(TreeNode node in e.Node.Nodes)
{
listView1.Items.Add(node.Text.ToString());
}
//--------------------------------------------------

you've been very helpful and I'm glad i've learnt alot. I may still
need a little more help as I've gotta display the nodes in the
listView in different formats as we do with windows explorer and to
display the information of the users in columns and finally edit the
users. this is what Iam going to start doing now.

Thanks for your help, you've been great.

Have a good day,
Kam
 
Back
Top