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

[Solved] Problems with binding a Hierarchal RadGrid

3 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nkululeko Mthethwa
Top achievements
Rank 1
Nkululeko Mthethwa asked on 05 Jan 2010, 12:18 PM
Hi,
I am using a Hierarchal RadGrid to display the list of classrooms in a school and a list of student in each classroom
but when I try to expand a classroom to show the students, my grid still show me the list of classrooms under a classroom instead of showing the lsit of students. I bind my grid to business objects, what am I doing wrong?
below is my code;
aspx:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server"
        </asp:ScriptManager> 
         
        <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None"  
            onneeddatasource="RadGrid1_NeedDataSource"
<MasterTableView> 
    <DetailTables> 
        <telerik:GridTableView runat="server" Name="details"
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px" /> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px" /> 
            </ExpandCollapseColumn> 
        </telerik:GridTableView> 
    </DetailTables> 
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn visible="True"
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
</MasterTableView> 
 
<FilterMenu EnableTheming="True"
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
</FilterMenu> 
        </telerik:RadGrid> 
         
    </div> 
    </form> 
</body> 
</html> 
 
c#
using System; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using Telerik.Web.UI; 
 
public partial class _Default : System.Web.UI.Page  
    protected void Page_Load(object sender, EventArgs e) 
    {    
        if (!Page.IsPostBack) 
        { 
            RadGrid1.DetailTableDataBind += new Telerik.Web.UI.GridDetailTableDataBindEventHandler(RadGrid1_DetailTableDataBind); 
        } 
    } 
    protected override void OnInit(EventArgs e) 
    { 
        base.OnInit(e); 
        if (!Page.IsPostBack) 
        { 
            RadGrid1.DetailTableDataBind += new Telerik.Web.UI.GridDetailTableDataBindEventHandler(RadGrid1_DetailTableDataBind); 
        } 
    } 
 
    void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e) 
    { 
        (source as RadGrid).DataSource = (e.DetailTableView.ParentItem.DataItem as ClassRoom).students; 
    } 
    public School MySchool 
    { 
        get 
        { 
            if (Session["school"] == null
                Session["school"] = new School(); 
            return Session["school"as School; 
        } 
    } 
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
    { 
        RadGrid1.DataSource = MySchool.classes; 
    } 
 
app_code(c#)
using System; 
using System.Data; 
using System.Configuration; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Collections.Generic; 
 
/// <summary> 
/// Summary description for Class1 
/// </summary> 
public class School 
    public List<ClassRoom> classes; 
    public School() 
    { 
        classes = new List<ClassRoom>(); 
        ClassRoom cr1 = new ClassRoom("Grade1"); 
        cr1.students.Add(new Student("Nifty""Morafo")); 
        cr1.students.Add(new Student("Nathi""Xulu")); 
        cr1.students.Add(new Student("Nkululeko""Nkuruza")); 
        classes.Add(cr1); 
 
        ClassRoom cr2 = new ClassRoom("Gravy"); 
        cr2.students.Add(new Student("Jerry""Skosana")); 
        cr2.students.Add(new Student("Brian""Baloyi")); 
        classes.Add(cr2); 
 
        ClassRoom cr3 = new ClassRoom("SuperGrade"); 
        cr3.students.Add(new Student("Anele""Ndlela")); 
        cr3.students.Add(new Student("Sindi""")); 
        classes.Add(cr3); 
 
        ClassRoom cr4 = new ClassRoom("Casanova"); 
        cr4.students.Add(new Student("Good""Night")); 
        classes.Add(cr4); 
    } 
public class ClassRoom 
    public ClassRoom(string gradeName) 
    { 
        students = new List<Student>(); 
        GradeName = gradeName; 
    } 
    public List<Student> students; 
 
    public string GradeName; 
    public string Grade 
    { 
        get 
        { 
            return GradeName; 
        } 
    } 
public class Student 
    public Student(string name, string surname) 
    { 
        this.Name = name; 
        this.Surname = surname; 
    } 
    public string FirstName 
    { 
        get { return Name; } 
    } 
    public string LastName 
    { 
        get { return Surname; } 
    } 
    public string Name; 
    public string Surname; 
Thanks in advance,

Nkululeko Mthethwa.



3 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 05 Jan 2010, 02:23 PM
Hello Nkululeko,

Try to change your code as shown bellow and let me know about the result:
C#:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
    
       if (!e.IsFromDetailTable)
          {
             RadGrid1.DataSource = MySchool.classes; 
          
    }

Additionally, I recommend you examine this online example, which elaborates on this subject:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx

I hope this helps.

Regards,
Pavlina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Nkululeko Mthethwa
Top achievements
Rank 1
answered on 05 Jan 2010, 04:03 PM
Hi Pavlina,
Thank you for a solution, however, I have another problem, when I expand the items, they show nothing at all.
0
Pavlina
Telerik team
answered on 06 Jan 2010, 12:41 PM
Hi Nkululeko,

Please, note that you need to define the ParentTableRelations/DataKeyNames for the MasterTableView/GridTableViews according to the database relations conventions.

For more information, please refer to the following link:
http://www.telerik.com/help/aspnet-ajax/grdhierarchicaldatabindingusingneeddatasource.html

Regards,
Pavlina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Nkululeko Mthethwa
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Nkululeko Mthethwa
Top achievements
Rank 1
Share this question
or