bala3569
bala3569

Reputation: 11010

binding data in asp.net treeview control?

I need a treeview structure like

Accounts-->Kmart-->California-->Stockton CA 95207
                             -->Oakdale, CA 95361   

from this table

SCID    SCEID   SCElement   ParentID    Parent      SalesChannelName
67      27      Account     0           Root        Kmart
71      28      State       67          KMart       California
72      29      Store       71          California  Stockton CA 95207
74      29      Store       71          California  Oakdale, CA 95361

i tried a sample from this link http://aspalliance.com/732. It is working fine.But i dont know how to (write a query to) form a tree structure from this table in that sample code..Any suggestion?

Upvotes: 0

Views: 1351

Answers (2)

PraveenVenu
PraveenVenu

Reputation: 8337

Fill the data from the table two times with different tablenames.

Ex:

da.Fill(ds,"Table1");
da.Fill(ds,"Table2");

Now add a data relation in the dataset with the ID from the Table1 and ParentId from Table2

Now Bind to the treeview

Below link will help you.

http://joshsmithonwpf.wordpress.com/2007/05/05/binding-a-treeview-to-a-dataset/

This uses two tables from DB. But you can use the same table twice with small change in the SQL query

Upvotes: 1

SeanCocteau
SeanCocteau

Reputation: 1876

In order to 'bind' data to a treeview, it must be in the correct format for the treeview to digest. That is it must apply the IHierarchicalDataSource interface such as a formatted XML Document, etc.

Unfortunately when you grab your data from the database it is not going to be hierarchically formatted leaving you with two options...

  1. Convert your data into an XMLDataSource and format / organise your data before binding to the treeview
  2. Just as in the example, load the data into a DataTable and use a recursive function to add the nodes manually. I guess if you're looking for encapsulation you could inherit a new control from the existing treeview and add the method accordingly.

HTH

Upvotes: 1

Related Questions