• 0

How best to populate node from datatable?


Question

Hi everyone. I am pretty new to C# and wanted to learn how to use the TreeView control since I've never used it before. My problem is that reading online about how to do different things hasn't made sense to me. I am trying to fill a datatable from my database and then add the rows to a particular node. I am figuring at this point that it would have to make use of a foreach, but that's about as far as I've gotten. So I was wondering if anyone who's done this might be able to explain it to me. I haven't really asked technical questions on here before, but everyone seems very helpful so I figured I would in this case. I don't even need the data in the datatable to be sorted into parents and children. I created 3 nodes in the designer and I just want to place the rows of data as children in these nodes. I am willing to do this as 3 different methods if need be. That's not a problem. I'm just trying to figure out how to get the data from my datatable into the TreeView.

Thanks to anyone that can help me with this.

1 answer to this question

Recommended Posts

  • 0

Ok, I assume you have a datatable; just replace "Code" and "Value" with the relevant columns in your datatable and the dt with the name of your datatable... this is a very basic example of how to populate some parent nodes.


foreach (DataRow row in dt.Rows) // For Every Row In DataTable
{
TreeNode myNode = new TreeNode(); // Create New Node Object
myNode.Text = row["Value"].ToString(); // Populate Visible Text Of Node
myNode.Name = row["Code"].ToString(); // Populate Name Of Object
treeView1.Nodes.Add(myNode); // Add Node To The Nodes Collection Of The TreeView Control
}
[/CODE]

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.