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: Index

1 Answer 1245 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Babu Puchakayala
Top achievements
Rank 1
Babu Puchakayala asked on 11 Jun 2010, 07:35 PM
Dear Telerik Team,

When I am trying to display data using radgrid I am getting error like "Index was out of range. Must be non-negative and less than the size of the collection. Parameter: Index".

Here is my code

<telerik:RadGrid ID="gvCktMap" BorderColor="White" runat="server" AutoGenerateColumns="true" 
                                AllowSorting="true" BackColor="White" AllowPaging="true" PageSize="25" GridLines="None" 
                                OnPageIndexChanging="gvCktMap_PageIndexChanging" OnRowCancelingEdit="gvCktMap_RowCancelingEdit" 
                                OnRowCommand="gvCktMap_RowCommand" OnRowUpdating="gvCktMap_RowUpdating" OnRowDataBound="gvCktMap_RowDataBound" 
                                OnSorting="gvCktMap_Sorting" OnRowEditing="gvCktMap_RowEditing" ShowGroupPanel="True" 
                                EnableHeaderContextMenu="true" EnableHeaderContextFilterMenu="true" AllowMultiRowSelection="true" 
                                AllowFilteringByColumn="True" AllowCustomPaging="false" OnItemCreated="gvCktMap_ItemCreated" 
                                EnableViewState="false" OnNeedDataSource="gvCktMap_NeedDataSource" OnItemUpdated="gvCktMap_ItemUpdated" 
                                > 
                                <MasterTableView DataKeyNames="sId" UseAllDataFields="true"
                                    <Columns> 
                                        <telerik:GridBoundColumn UniqueName="sId" HeaderText="sId" DataField="sId" Visible="false"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="orderId" HeaderText="orderId" DataField="orderId"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="REJ" HeaderText="REJ" DataField="REJ"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="Desc" HeaderText="Desc" DataField="Desc"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="CustomerName" HeaderText="CustomerName" DataField="CustomerName"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="MarketName" HeaderText="MarketName" DataField="MarketName"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="HeadendName" HeaderText="HeadendName" DataField="HeadendName"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="SiteName" HeaderText="SiteName" DataField="SiteName"
                                        </telerik:GridBoundColumn> 
                                        <telerik:GridBoundColumn UniqueName="TaskStatus" HeaderText="TaskStatus" DataField="TaskStatus"
                                        </telerik:GridBoundColumn> 
                                    </Columns>
                                 </mastertableview>
                            </telerik:RadGrid> 


Here is my code behind

private void bingGrid() 
    { 
        try 
        { 
            gvCktMap.Columns.Clear(); 
            DataSet dsResult = new DataSet(); 
            DataSet dsEditItems = new DataSet(); 
            dsEditItems.ReadXml(Server.MapPath("XMLS/" + Session["TaskID"].ToString() + ".xml")); 
 
            clsSearch_BL clsObj = new clsSearch_BL(); 
            clsObj.TaskID = (string)Session["TaskID"]; 
            clsObj.CustName = (string)Session["CustName"]; 
            clsObj.MarketName = (string)Session["MarketName"]; 
            clsObj.HeadendName = (string)Session["HeadendName"]; 
            clsObj.SiteName = (string)Session["SiteName"]; 
            clsObj.TaskStatus = (string)Session["TaskStatus"]; 
            clsObj.OrdType = (string)Session["OrdType"]; 
            clsObj.OrdStatus = (string)Session["OrdStatus"]; 
            clsObj.ProName = (string)Session["ProName"]; 
            clsObj.LOC = (string)Session["LOC"]; 
 
            dsResult = clsObj.getSearchResults_BL(clsObj); 
            Session["SearchRes"] = dsResult; 
            DataTable dtFilter = new DataTable(); 
            DataColumn dtCol = new DataColumn("FilterBy"); 
            dtFilter.Columns.Add(dtCol); 
            dtCol = new DataColumn("DataType"); 
            dtFilter.Columns.Add(dtCol); 
            gvCktMap.DataSource = dsResult
            gvCktMap.DataBind(); 
    } 
        catch (Exception ex) 
        { 
        } 
    } 


If i remove <MasterTableView></MasterTableView> Its working fine without any error. But for some reasons i need to use <MasterTableView></MasterTableView> Can anyone help me out to fix this error.

Thanks In Advance



1 Answer, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 16 Jun 2010, 11:31 AM
Hi Babu,

In order to make sure that the exception does not happen, you need to create the structure of the control properly:

http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

If you would like to alter the structure dynamically, you can use the following approach:

http://www.telerik.com/help/aspnet-ajax/grdchanginggridstructuredynamically.html

Alternatively, you can declare the whole structure statically, in the aspx page.
Also, in order to make sure that the operations in the control are performed as expected, you need to take advantage of the NeedDataSource event handler, or a datasource control, to populate the grid with data.
I hope this information gets you started properly.

Best wishes,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Babu Puchakayala
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or