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.