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

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

1 Answer 283 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Taye
Top achievements
Rank 1
Taye asked on 30 Sep 2012, 03:11 PM
I'm trying to do Hierarchical Grid using two inner detail table, and the second table has also hierarchy.For the first hierarchy it works fine but while I'm trying to expand the second inner table it raise  Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index ,
I have put my code sample here
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <%--Needed for JavaScript IntelliSense in VS2010--%>
            <%--For VS2008 replace RadScriptManager with ScriptManager--%>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <script type="text/javascript">
        //Put your JavaScript code here.
    </script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <div>
     <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
           OnDetailTableDataBind="RadGrid1_DetailTableDataBind">
             <MasterTableView DataKeyNames="ID">
                 <DetailTables>
                   <telerik:GridTableView AutoGenerateColumns="True" Caption="Items" AllowSorting="false"
                     Width="100%" PageSize="7"  DataMember="Items">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="MasterItemID" MasterKeyField="ID" />
                    </ParentTableRelation>
                    </telerik:GridTableView>
                     <telerik:GridTableView AutoGenerateColumns="True" Caption="Sub Kit" AllowSorting="false"
                     Width="100%" PageSize="7"  DataMember="Details">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="MasterID" MasterKeyField="ID" />
                    </ParentTableRelation>
                    <DetailTables>
                     <telerik:GridTableView AutoGenerateColumns="True" Caption="sub Items" AllowSorting="false"
                     Width="100%" PageSize="7"  DataMember="SubKitItems">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="ParentID" MasterKeyField="ID" />
                    </ParentTableRelation>
                    </telerik:GridTableView>
                     <telerik:GridTableView AutoGenerateColumns="True" Caption="Sub Kit" AllowSorting="false"
                     Width="100%" PageSize="7"  DataMember="SubKitDetails">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="ParentID" MasterKeyField="ID" />
                    </ParentTableRelation>
                    </telerik:GridTableView>
                    </DetailTables>
                    </telerik:GridTableView>
                 </DetailTables>
                 
             </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
 
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        if(e.IsFromDetailTable)
            return;
        RadGrid1.DataSource = MyList;
    }
 
    protected void RadGrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
        if (e.DetailTableView.DataMember == "Details")
        {
            e.DetailTableView.DataSource = MyList.Find(
                delegate(Master master)
                {
                    return master.ID == Convert.ToInt32(e.DetailTableView.ParentItem.GetDataKeyValue("ID"));
                }
                ).Details;
        }
        if (e.DetailTableView.DataMember == "Items")
        {
            int parentId = Convert.ToInt32(e.DetailTableView.ParentItem.GetDataKeyValue("ID"));
            e.DetailTableView.DataSource = MyList.Find(
                delegate(Master master)
                {
                    return master.ID == Convert.ToInt32(e.DetailTableView.ParentItem.GetDataKeyValue("ID"));
                }
                ).Items;
        }
        if (e.DetailTableView.DataMember == "SubKitDetails")
        {
            e.DetailTableView.DataSource = MyList.Find(
                delegate(Master master)
                {
                    return master.ID == Convert.ToInt32(e.DetailTableView.ParentItem.GetDataKeyValue("ID"));
                }
                ).SubKitDetails;
           
        }
 
        if (e.DetailTableView.DataMember == "SubKitItems")
        {
            e.DetailTableView.DataSource = MyList.Find(
                delegate(Master master)
                {
                    return master.ID == Convert.ToInt32(e.DetailTableView.ParentItem.GetDataKeyValue("ID"));
                }
                ).SubKitItems;
        }
    }
 
 
 
    public List<Master> MyList
    {
        get
        {
            List<Master> items = new List<Master>();
            for (int i = 0; i < 5; i++)
            {
                Master item = new Master();
                item.ID = i;
                item.Name = String.Format("Item{0}", i);
 
                List<Detail> detailList = new List<Detail>();
                List<Items> itemlist = new List<Items>();
                //sub items
                List<Detail> subKitDetails = new List<Detail>();
                List<Items> subKitItems = new List<Items>();
 
                for (int j = 0; j < 5; j++)
                {
                    Detail detailItem = new Detail();
                    Items myItem = new Items();
                    detailItem.ID = j;
                    detailItem.Name = String.Format("Item{0}", j);
                    detailItem.MasterID = i;
                    detailList.Add(detailItem);
                    myItem.ID = j;
                    myItem.Name = String.Format("Item{0},{1}", j, "Test Item");
                    myItem.MasterItemID = i;
                    itemlist.Add(myItem);
                    subKitDetails = GetSubKitDetails(i, j);
                    subKitItems = GetSubItems(i, j);
                }
 
                item.Details = detailList;
                item.Items = itemlist;
                item.SubKitDetails = subKitDetails;
                item.SubKitItems = subKitItems;
                items.Add(item);
            }
 
            return items;
        }
    }
 
    private List<Items> GetSubItems(int i, int j)
    {
        List<Items> subKitItems = new List<Items>();
        for (int k = 0; k < 5; k++)
        {
            Items myItem = new Items();
            myItem.ID = k;
            myItem.MasterItemID=i;
            myItem.Name = String.Format("Item{0},{1}", k, "Test subItem");
            myItem.ParentID = i;
            myItem.SubParentID = j;
            subKitItems.Add(myItem);
 
        }
        return subKitItems;
    }
 
    private List<Detail> GetSubKitDetails(int i, int j)
    {
        List<Detail> subKitDetails = new List<Detail>();
        for (int k = 0; k < 5; k++)
        {
            Detail detailItem = new Detail();
            detailItem.MasterID = i;
            detailItem.ID = k;
            detailItem.Name = String.Format("Item{0},{1}", k, "Test sub details");
            detailItem.ParentID = i;
            detailItem.SubParentID = j;
            subKitDetails.Add(detailItem);
 
        }
        return subKitDetails;
    }
}
    public class Master
    {
        public int ID { get; set; }
        public int ParentID { get; set; }
        public string Name { get; set; }
        public List<Detail> Details { get; set; }
        public List<Items> Items { get; set; }
        public List<Detail> SubKitDetails { get; set; }
        public List<Items> SubKitItems { get; set; }
    }
 
    public class Detail : Master
    {
        public int MasterID { get; set; }
        public int SubParentID { get; set; }    
    }
 
    public class DetailSecond : Master
    {
        public int MasterDetailID { get; set; }
        public int ParentID { get; set; }
    }
 
 
    public class Items : Master
    {
        public int MasterItemID { get; set; }
        public int SubParentID { get; set; }
 
    }
Thanks,

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 03 Oct 2012, 02:28 PM
Hello,

My colleague Angel have answered your query in the support thread you have opened. The solution is to add the field that you are using for the ParentTableRelation to the DataKeyNames collection.

If the license holder add you as a licensed developer you will be able to get priority support.

All the best,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Taye
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or