|
-
Aug 16th, 2012, 01:26 AM
#1
Thread Starter
Junior Member
Need Help
Hi ,
I have a data table 'CashBook' in database , i have one tree view in winform using c# , i already made a method by which the datatable is come into dataset .
public DataTable ShowTreeView()
{
SqlConnection connection = dl.OpenConnection();
string selectLegderHead = "select * from CashBook ";
SqlDataAdapter adapter = new SqlDataAdapter(selectLegderHead, connection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
return dataset.Tables[0];
}
how i can add values to tree view or bound it to datatable .
my tree view name is treeview1 .
-
Aug 16th, 2012, 02:31 AM
#2
Re: Need Help
The WinForms TreeView doesn't support binding the way a DataGridView does. You can't just set a DataSource property and have it all done for you. You need to simply loop through the Rows of the DataTable and, for each one, Add a TreeNode to the Nodes of the TreeView. I would suggest that you should create all the nodes and add them to an array first and then call AddRange to add them all in one go.
csharp Code:
var nodes = new TreeNode[table.Rows.Count] {}; for (var i = 0; i < table.Rows.Count; i++) { var row = table.Rows(i); nodes(i) = new TreeNode(...); } tree.Nodes.AddRange(nodes);
You could also use a bit of LINQ to simplify the code but you really should understand this way first before hiding the details behind LINQ.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|