GetEmployeeDetails() method returns the value of type Employee. I need to display that value in Telerik RadGrid such a way that, when we click on Employee rows it should expands related Address class fields row under the Employee row. Below is the structure of Employee class.
class Employee
{
string EmpId;
string Name;
int Age;
List<Address> address;
}
class Address
{
string Street;
string City;
int Zip;
}
I have written below code to display Employee details at run time in ASP page using Telerik RadGrid. But its displaying only first level- Employee class fields and Address fields with empty. Could you please help me to come out of this issue…?
protected void Page_Load(object sender, EventArgs e)
{
List<Employee> empList = GetEmployeeDetails();
DataSet dataset = new DataSet("DataSet");
System.Data.DataTable dt1 = new System.Data.DataTable();
dt1.TableName = "Employee";
dt1.Columns.Add("EmpId");
dt1.Columns.Add("Name");
dt1.Columns.Add("Age");
dataset.Tables.Add(dt1);
System.Data.DataTable dt2 = new System.Data.DataTable();
dt2.TableName = "Address";
dt2.Columns.Add("EmpId");
dt2.Columns.Add("Street");
dt2.Columns.Add("City");
dt2.Columns.Add("Zip");
dataset.Tables.Add(dt2);
foreach (Employee emp in empList)
{
dt1.Rows.Add(new object[] { emp.empId, emp.name, emp.age });
foreach (Address add in emp.address)
{
dt2.Rows.Add(new object[] {emp.empId, add.street, add.city, add.zip });
}
}
DataRelation rel = new DataRelation("rel", dataset.Tables["Employee"].Columns["EmpId"], dataset.Tables["Address"].Columns["EmpId"]);
dataset.Relations.Add(rel);
RadGrid1.DataSource = dataSet;
RadGrid1.DataBind();
}
Per the Telerik site, you must use the “RadGrid1_NeedDataSource” event to do your binding if you wish to have a hierarchy.
I got it to work by doing the following. (BTW I used your classes and code so it should be easy to mimic as I will post all the code. You also left out the method GetEmployeeDetails(), so I made my own:
Step 1: Define the grid’s hierarchy view:
Step 2: Set your data sources: (No need for DataBind method to be called or relations added, that’s done by the grid)
Step 3: Run it.
Obviously you may need to make adjustments to your casing as there may be slight capitalization differences. I also adjusted you classes to allow setting of variable data, nothing big:
Hope this helps.
-JJ