Results 1 to 2 of 2

Thread: Need Help

  1. #1
    Junior Member
    Join Date
    Jul 12
    Posts
    22

    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 .

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,872

    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:
    1. var nodes = new TreeNode[table.Rows.Count] {};
    2.  
    3. for (var i = 0; i < table.Rows.Count; i++)
    4. {
    5.     var row = table.Rows(i);
    6.  
    7.     nodes(i) = new TreeNode(...);
    8. }
    9.  
    10. 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
  •