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

Grid Binding List within List

1 Answer 229 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 03 Aug 2013, 08:33 PM
Good Afternoon,

I am currently working on a project at work and we are using the RadGrid to display some hierarchical information. All works fine except for binding to a list that is contained within the primary class. I have the following:

Approver Class:
---------------------

    public class BOC_Approver
    {
        public BOC_Approver()
        {
        }

        public BOC_Approver(Int32 MyApproverID, String MyApproverName)
        {
            ApproverID   = MyApproverID;
            ApproverName = MyApproverName;
        }

        public Int32  ApproverID { get; set; }
        public String ApproverName { get; set; }
    }



Product Class:
-------------------
    public class BOC_Product
    {
        public BOC_Product()
        {
            Approvers = new List<BOC_Approver>();
        }

        public BOC_Product(Int32 MyProductID, String MyName) : this()
        {
            ProductID = MyProductID;
            ProductName = MyName;         
        }

        public Int32              ProductID { get; set; }
        public String             ProductName { get; set; }
        public List<BOC_Approver> Approvers { get; set; }
     }


Rad Grid Setup:
---------------------
  <telerik:RadGrid ID="gridNewHireRequests" runat="server" CssClass="MyGridControl" GridLines="None" AutoGenerateColumns="False"
                             OnNeedDataSource="gridNewHireRequests_NeedDataSource" >                        
        <ClientSettings>
          <Scrolling AllowScroll="True" ScrollHeight="100%" UseStaticHeaders="false" />
        </ClientSettings>
                        
        <MasterTableView DataKeyNames="ProductID" CommandItemDisplay="Top">
            <CommandItemSettings ShowAddNewRecordButton="true" ShowRefreshButton="false" />
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="ProductID" Width="100%" runat="server">
                        <ParentTableRelation>
                            <telerik:GridRelationFields DetailKeyField="ProductID" MasterKeyField="ProductID" />
                        </ParentTableRelation>
                        <Columns>
                          <telerik:GridBoundColumn  DataField="Approvers.ApproverID" HeaderText="Approver ID" UniqueName="ApproverID" />
                          <telerik:GridBoundColumn   DataField="Approvers.ApproverName"  HeaderText="Approver Name"  UniqueName="ApproverName" />
                         </Columns>
                        </telerik:GridTableView>
                    </DetailTables>
                    <Columns>
                        <telerik:GridBoundColumn DataField="ProductID" HeaderText="Product ID" UniqueName="ProductID" />
                        <telerik:GridBoundColumn DataField="ProductName" HeaderText="Product Name" UniqueName="ProductName" />
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>


If the Approvers List in the Product class was just another class I can get this to work no issues, but because the Approvers property in the Product class is a list I am unsure how to get it to bind in the subgrid. At the moment if throws an error with unknown fields of course.

is there a standard way to handle binding if a list that each element contains another list?

Thanks, Any help would be appreciated.

Chris Eisnaugle

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Aug 2013, 06:22 AM
Hello,

Please try with the below code snippet.

<telerik:RadGrid ID="gridNewHireRequests" runat="server" AutoGenerateColumns="False"
       OnNeedDataSource="gridNewHireRequests_NeedDataSource" OnDetailTableDataBind="gridNewHireRequests_DetailTableDataBind">
       <ClientSettings>
           <Scrolling AllowScroll="True" ScrollHeight="100%" UseStaticHeaders="false" />
       </ClientSettings>
       <MasterTableView DataKeyNames="ProductID" CommandItemDisplay="Top">
           <CommandItemSettings ShowAddNewRecordButton="true" ShowRefreshButton="false" />
           <DetailTables>
               <telerik:GridTableView DataKeyNames="ApproverID" Width="100%" runat="server">
                   <Columns>
                       <telerik:GridBoundColumn DataField="ApproverID" HeaderText="Approver ID" UniqueName="ApproverID" />
                       <telerik:GridBoundColumn DataField="ApproverName" HeaderText="Approver Name" UniqueName="ApproverName" />
                   </Columns>
               </telerik:GridTableView>
           </DetailTables>
           <Columns>
               <telerik:GridBoundColumn DataField="ProductID" HeaderText="Product ID" UniqueName="ProductID" />
               <telerik:GridBoundColumn DataField="ProductName" HeaderText="Product Name" UniqueName="ProductName" />
           </Columns>
       </MasterTableView>
   </telerik:RadGrid>
public partial class Forum : System.Web.UI.Page
{
    protected void Page_Init(object sender, System.EventArgs e)
    {
 
    }
 
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            List<BOC_Product> BOC_Products = new List<BOC_Product>();
 
            for (int i = 0; i < 5; i++)
            {
                BOC_Product bp = new BOC_Product();
 
                bp.ProductID = i;
                bp.ProductName = "product" + i;
 
                List<BOC_Approver> BOC_Approvers = new List<BOC_Approver>();
 
                for (int j = 0; j < 5; j++)
                {
                    BOC_Approver ba = new BOC_Approver();
                    ba.ApproverID = i;
                    ba.ApproverName = "product:" + i + "_apporve:" + j;
                    BOC_Approvers.Add(ba);
                }
 
                bp.Approvers = BOC_Approvers;
                BOC_Products.Add(bp);
            }
 
            Session["GridData"] = BOC_Products;
        }
    }
 
    protected void Page_PreRender(object sender, System.EventArgs e)
    {
 
    }
    protected void gridNewHireRequests_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        List<BOC_Product> BOC_Products = new List<BOC_Product>();
 
        if (Session["GridData"] != null)
        {
            BOC_Products = (List<BOC_Product>)Session["GridData"];
        }
 
        gridNewHireRequests.DataSource = BOC_Products;
    }
    protected void gridNewHireRequests_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
 
        List<BOC_Approver> Approvers = new List<BOC_Approver>();
        int ProductID = Convert.ToInt32((e.DetailTableView.ParentItem as GridDataItem).GetDataKeyValue("ProductID").ToString());
        if (Session["GridData"] != null)
        {
            List<BOC_Product> BOC_Products = (List<BOC_Product>)Session["GridData"];
            Approvers = BOC_Products.Where(i => i.ProductID == ProductID).FirstOrDefault().Approvers;
        }
 
        e.DetailTableView.DataSource = Approvers;
    }
}
 
public class BOC_Approver
{
    public BOC_Approver()
    {
    }
 
    public BOC_Approver(Int32 MyApproverID, String MyApproverName)
    {
        ApproverID = MyApproverID;
        ApproverName = MyApproverName;
    }
 
    public Int32 ApproverID { get; set; }
    public String ApproverName { get; set; }
}
 
public class BOC_Product
{
    public BOC_Product()
    {
        Approvers = new List<BOC_Approver>();
    }
 
    public BOC_Product(Int32 MyProductID, String MyName)
        : this()
    {
        ProductID = MyProductID;
        ProductName = MyName;
    }
 
    public Int32 ProductID { get; set; }
    public String ProductName { get; set; }
    public List<BOC_Approver> Approvers { get; set; }
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or