views:

63

answers:

2

Work on asp.net vs 05 C#. I read many books on use of treeviews and they all display sitemaps.

However I want it to display my own database for other purpose - eg supervisor-sunordinates/reports relation.

Suppose my database is like this

Supervisor,Staff
===============
Peter, Mary
Peter, Tom
Winnie, Victor
etc


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_ChildTable_ParentTable]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE [dbo].[ChildTable] DROP CONSTRAINT FK_ChildTable_ParentTable
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ParentTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[ParentTable]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ChildTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[ChildTable]
GO

CREATE TABLE [dbo].[ParentTable] (
 [ParentID] [int] NOT NULL ,
 [ParentName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[ChildTable] (
 [ParentID] [int] NOT NULL ,
 [ChildName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL 
) ON [PRIMARY]
GO

I have more than one supervisor ,I want Supervisor to be 1st level nodes then supervisor name and staff would under respective nodes of their supervisor show .When i click a supervisor name than show that page with out postback ,want to use the ajax ,when click staff then show that page with out postback.

Supervisor //Parent node.Click on nothing will happen
   Peter   //Supservisor Name.Click on  show  Supervisor Peter Page.
     Marry //Staff Name.Click on Show staff Marry Page.
     Tom
   Winnie  //Supervisor Name
      Victor

What are the codes to bind Treeview to this sqldatasource ?

Thanks

A: 

you could try this code...

DataTable dtbl1=new DataTable();//parent datatable
    DataTable dtbl2=new DataTable();//child datatable

    DataSet ds = new DataSet();
    ds.Tables.Add(dtbl1);
    ds.Tables.Add(dtbl2);
    ds.Relations.Add("Children", dtbl1.Columns["dtb1ID"], dtbl2.Columns["dtbl2ID"]);//define parent child relation in dataset

    if (ds.Tables[0].Rows.Count > 0)
    {
        trv.Nodes.Clear();
        Int32 count = 0;

        foreach(DataRow masterRow in  ds.Tables[0].Rows)
        {
            TreeNode masterNode = new TreeNode((String)masterRow["dtbl1ColumnYouWantToDisplay"], Convert.ToString(masterRow["dtbl1ID"]));
            trv.Nodes.Add(masterNode);

            foreach (DataRow childRow in masterRow.GetChildRows("Children"))
            {
                TreeNode childNode = new TreeNode((String)childRow["dtbl2ColumnYouWantToDisplay"], Convert.ToString(childRow["dtb2ID"]));
                masterNode.ChildNodes.Add(childNode);
                count++;
            }
        }
        trv.ExpandAll();
    }
Muhammad Akhtar
A: 

Hello

Maybe you can try ASTreeView, Powerful and FREE.

Features:

drag and drop

ajax add, edit, delete

dynamic loading

custom icons

three state checkbox

and many more...

http://www.astreeview.com

Weijie JIN