This is a migrated thread and some comments may be shown as answers.

Creating multilevel Hierarchy grid in codebehind

1 Answer 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joel
Top achievements
Rank 1
Joel asked on 03 Feb 2012, 08:02 PM
Hi there everyone........Im trying to create a grid with dynamic hierarchy drill down levels in code behind....this is my code




SqlConnection cn = new SqlConnection(connStr);
            SqlDataAdapter da = new SqlDataAdapter("SELECT MAX(node) AS node FROM tabs", cn);
            DataTable dt = new DataTable();
 
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            try
            {
                da.Fill(dt);
            }
            catch { }
 
            if (dt != null && dt.Rows.Count > 0)
            {
                Int32 node = Convert.ToInt32(dt.Rows[0]["node"].ToString());
 
                RadGrid RadGrid1 = new RadGrid();
 
                SqlDataSource sdsMaster = new SqlDataSource(connStr,"SELECT * FROM tabs WHERE active=1 AND idParentNode = '0' ORDER BY displayOrder");
                sdsMaster.ID = "sqlDataSourceMaster";
                phGridContainer.Controls.Add(sdsMaster);
 
                RadGrid1.ID = "RadGrid1";
                RadGrid1.DataSourceID = "SqlDataSourceMaster";
 
                RadGrid1.MasterTableView.DataKeyNames = new string[] { "id" };
 
                RadGrid1.Width = Unit.Percentage(98);
                RadGrid1.PageSize = 3;
                RadGrid1.AllowPaging = true;
                RadGrid1.AllowSorting = true;
                RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
                RadGrid1.AutoGenerateColumns = false;
                RadGrid1.ShowStatusBar = true;
 
                RadGrid1.MasterTableView.PageSize = 10;
 
                //Add columns
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "id";
                boundColumn.HeaderText = "ID";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "label";
                boundColumn.HeaderText = "Tab";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
 
                 
 
                if (node > 0)
                {
                    for (int i = 1; i <= node; i++)
                    {
                        SqlDataSource sds = new SqlDataSource(connStr,"SELECT * FROM Tabs WHERE active=1 AND idParentNode = @id ORDER BY displayOrder");
                        /*THIS IS THE PART IM HAVING ISSUES*/
                        sds.SelectParameters.Add("id", RadGrid1.MasterTableView.DataKeyValues[0].ToString());
                         
                        sds.ID = "sqlDataSource" + i.ToString();
                         
                        phGridContainer.Controls.Add(sds);
                        
                        GridTableView tableViewDetail = new GridTableView(RadGrid1);
                        tableViewDetail.DataSourceID = "sqlDataSource" + i.ToString();
                        tableViewDetail.Width = Unit.Percentage(100);
 
                        tableViewDetail.DataKeyNames = new string[] { "id" };
 
                        GridRelationFields relationFields = new GridRelationFields();
                        relationFields.MasterKeyField = "id";
                        relationFields.DetailKeyField = "idParentNode";
                        tableViewDetail.ParentTableRelation.Add(relationFields);
 
                        RadGrid1.MasterTableView.DetailTables.Add(tableViewDetail);
 
                        //Add columns
                        boundColumn = new GridBoundColumn();
                        boundColumn.DataField = "id";
                        boundColumn.HeaderText = "ID";
                        tableViewDetail.Columns.Add(boundColumn);
 
                        boundColumn = new GridBoundColumn();
                        boundColumn.DataField = "label";
                        boundColumn.HeaderText = "Tab";
                        tableViewDetail.Columns.Add(boundColumn);
                         
                         
                    }
                     
                     
                }
                 
                phGridContainer.Controls.Add(RadGrid1);
            }
            cn.Close();


How can i get the Parent datakey to be able to insert that parameter within my dynamically generated sqldatasource.....Is it possible to have this done using radgrid control?? I know i can use the Tree View control but I the requirements fit the rad grid one.

Thks in advance

1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 07 Feb 2012, 01:21 PM
Hi Joe,

Have you gone through the following online example:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/hierarchy/defaultcs.aspx 

The only thing that you need to do is to correctly set the name of the sqldatasource select parameters (as shown in the example, the only difference being that you will do so in code-behind).

Regards,
Tsvetoslav
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Grid
Asked by
Joel
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or