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

[Solved] Create Hierarchical RadGird from server side using GridTableView

1 Answer 121 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Qaja Mohiddin
Top achievements
Rank 1
Qaja Mohiddin asked on 14 May 2009, 04:48 PM
Hi,
I want to create a RADGrid with a Hierarchical view, from server side. i.e
i want to bind the Grid with my dataset, and form a relation with the GridTableView on some column.
since my queries are based on paramters.
The below is my code, pls tell me wht is the error in it,
i am able to bind data to MainGrid but the Hierarchical grid is not forming a realtion with main grid.

 

using

 

System;

 

using

 

Telerik.Web.UI;

 

using

 

System.Web.UI.WebControls;

 

 

using

 

System.Data;

 

using

 

System.Data.SqlClient;

 

using

 

System.Configuration;

 

using

 

System.Collections;

 

using

 

System.Web;

 

using

 

System.Web.Security;

 

using

 

System.Web.UI;

 

using

 

System.Web.UI.WebControls.WebParts;

 

using

 

System.Web.UI.HtmlControls;

 

namespace

 

Telerik.GridExamplesCSharp.Programming.Hierarchy

 

{

 

public partial class OneView : System.Web.UI.Page

 

{

 

private void DefineGridStructure()

 

{

 

 

RadGrid RadGrid1 = new RadGrid();

 

RadGrid1.ID =

"RadGrid1";

 

 

 

//RadGrid1.DataSourceID = "SqlDataSource1";

 

RadGrid1.AutoGenerateColumns =

false;

 

RadGrid1.Skin =

"Web20";

 

RadGrid1.MasterTableView.DataKeyNames =

new string[] { "CustomerID" };

 

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 = 3;

 

//Add columns

 

 

GridBoundColumn boundColumn;

 

boundColumn =

new GridBoundColumn();

 

boundColumn.DataField =

"CustomerID";

 

boundColumn.HeaderText =

"CustomerID";

 

RadGrid1.MasterTableView.Columns.Add(boundColumn);

boundColumn =

new GridBoundColumn();

 

boundColumn.DataField =

"ContactName";

 

boundColumn.HeaderText =

"Contact Name";

 

RadGrid1.MasterTableView.Columns.Add(boundColumn);

RadGrid1.DataSource = MakeDS().Tables[0];

RadGrid1.DataBind();

 

 

 

            //Detail table - Orders (II in hierarchy level)

 

 

GridTableView tableViewOrders = new GridTableView(RadGrid1);

 

tableViewOrders.AutoGenerateColumns =

false;

 

 

//tableViewOrders.DataSourceID = "SqlDataSource2";

 

tableViewOrders.da

 

 

////Add columns

 

boundColumn =

new GridBoundColumn();

 

boundColumn.DataField =

"OrderID";

 

boundColumn.HeaderText =

"OrderID";

 

tableViewOrders.Columns.Add(boundColumn);

boundColumn =

new GridBoundColumn();

 

boundColumn.DataField =

"OrderDate";

 

boundColumn.HeaderText =

"Date Ordered";

 

tableViewOrders.Columns.Add(boundColumn);

tableViewOrders.DataSource = MakeDS().Tables[1];

tableViewOrders.DataBind();

tableViewOrders.Width =

Unit.Percentage(100);

 

tableViewOrders.DataKeyNames =

new string[] { "OrderID" };

 

 

 

GridRelationFields relationFields = new GridRelationFields();

 

relationFields.MasterKeyField =

"CustomerID";

 

relationFields.DetailKeyField =

"CustomerID";

 

tableViewOrders.ParentTableRelation.Add(relationFields);

RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders);

 

//Add the RadGrid instance to the controls

 

 

this.PlaceHolder1.Controls.Add( RadGrid1 );

 

}

 

 

protected void Page_Init(object source, System.EventArgs e)

 

{

DefineGridStructure();

}

 

protected void Page_Load(object sender, EventArgs e)

 

{

 

RadGrid Grid1 = (RadGrid)PlaceHolder1.FindControl("RadGrid1");

 

 

 

 

}

 

public DataSet MakeDS()

 

{

 

SqlDataAdapter DA = new SqlDataAdapter();

 

 

DataSet DS = new DataSet();

 

 

SqlCommand cmd;

 

 

string strconnection =

 

 

@"Data Source=MY\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";

 

 

SqlConnection sqlcon = new SqlConnection();

 

sqlcon.ConnectionString = strconnection;

cmd =

new SqlCommand("SELECT * FROM Customers", sqlcon);

 

cmd.Connection.Open();

 

DA.SelectCommand = cmd;

 

//creating datasets

 

 

DA.Fill(DS,

"MainTable");

 

 

cmd =

new SqlCommand("SELECT * FROM Orders ", sqlcon);

 

DA.SelectCommand = cmd;

DA.Fill(DS,

"SecondTable");

 

cmd.Connection.Close();

 

return DS;

 

}

}

}


Pls help me out!
Thanks for your help in advance.

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 18 May 2009, 01:07 PM
Hi Qaja,

From your code snippets I see that you use simple binding with DataBind() calls for your grid. Note that this type of binding does not support hierarchical structure and you will need to switch to advanced binding with data source controls or DetailTableDataBind event handling as explained here.

The two methods for defining hierarchical relations are outlined in these resources:

http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/declarativerelations/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx

http://www.telerik.com/help/aspnet-ajax/grdhierarchicaldatabindingusingneeddatasource.html
http://www.telerik.com/help/aspnet-ajax/grdhierarchicaldatabindingusingdetailtabledatabind.html

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Qaja Mohiddin
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or