Charan Raju C R
Charan Raju C R

Reputation: 768

How to display hierarchical class fields in Telerik RadGrid?

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();

    }

Upvotes: 2

Views: 2341

Answers (1)

KreepN
KreepN

Reputation: 8598

Per the Telerik site, you must use the "RadGrid1_NeedDataSource" event to do your binding if you wish to have a hierarchy.

enter image description here

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:

   private List<Employee> GetEmployeeDetails()
   {
        List<Employee> myEmployees = new List<Employee>();

        Employee Steve = new Employee()
            {
                Address = new List<Address>() { new Address { City = "op", Street = "thatstreet", Zip = 23312 } },
                Age = 23,
                EmpId = "Emp1",
                Name = "SteveIsTheName"
            };


        Employee Carol = new Employee()
            {
                Address = new List<Address>() {
                    new Address { City = "op2", Street = "thatstreet2", Zip = 23313 },
                    new Address { City = "op3", Street = "thatstreet3", Zip = 23314 }},
                Age = 24,
                EmpId = "Emp2",
                Name = "CarolIsTheName"
            };

        myEmployees.Add(Steve);
        myEmployees.Add(Carol);

        return myEmployees;
    }

Step 1: Define the grid's hierarchy view:

protected void RadGrid1_Init(object sender, EventArgs e)
{
    DefineGridStructure();
}

private void DefineGridStructure()
{
    RadGrid1.MasterTableView.DataKeyNames = new string[] { "EmpId" };
    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 = "EmpId";
    boundColumn.HeaderText = "EmpId";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Name";
    boundColumn.HeaderText = "Name";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Age";
    boundColumn.HeaderText = "Age";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);

    //Detail table - Orders (II in hierarchy level)
    GridTableView tableViewOrders = new GridTableView(RadGrid1);
    tableViewOrders.Width = Unit.Percentage(100);
    tableViewOrders.DataKeyNames = new string[] { "EmpId" };

    GridRelationFields relationFields = new GridRelationFields();
    relationFields.MasterKeyField = "EmpId";
    relationFields.DetailKeyField = "EmpId";
    tableViewOrders.ParentTableRelation.Add(relationFields);
    RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders);

    //Add columns
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Street";
    boundColumn.HeaderText = "Street";
    tableViewOrders.Columns.Add(boundColumn);

    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "City";
    boundColumn.HeaderText = "City";
    tableViewOrders.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Zip";
    boundColumn.HeaderText = "Zip";
    tableViewOrders.Columns.Add(boundColumn);
}

Step 2: Set your data sources: (No need for DataBind method to be called or relations added, that's done by the grid)

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs 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 });
            }
        }

        RadGrid1.MasterTableView.DataSource = dataset.Tables["Employee"];
        RadGrid1.MasterTableView.DetailTables[0].DataSource = dataset.Tables["Address"];

    }

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:

class Employee
{
    public List<Address> Address { get; set; }

    public int Age { get; set; }

    public string Name { get; set; }

    public string EmpId { get; set; }
}

class Address
{
    public string Street { get; set; }

    public string City { get; set; }

    public int Zip { get; set; }
}

Hope this helps.

-JJ

Upvotes: 4

Related Questions