Home > DeveloperSection > Forums > Index was out of range in grid row command
Jeet Verma
Jeet Verma

Total Post:110

Posted on    September-01-2014 12:07 AM


 1 Reply(s)
 689  View(s)
Rate this:

I'd like to reach gridView a cell's value.But it gives the error is 'Index was out of range. Must be non-negative and less than the size of the collection. grid row command'

Can somebody help me?

 <asp:GridView ID="grdList" runat="server" AutoGenerateColumns="False"  GridLines="None"

            PageSize="5" AllowPaging="True" OnPageIndexChanging="Grd_Allergys_PageIndexChanging"

            OnRowCommand="grdList_RowCommand" OnRowDeleting="grdList_RowDeleting" OnRowEditing="grdList_RowEditing">


                <asp:BoundField DataField="LangId" HeaderText="LangId" />

                <asp:BoundField DataField="LangName" HeaderText="LangName" />

                <asp:TemplateField HeaderText="Edit">


                        <asp:ImageButton ID="imgBtnEdit" runat="server" CommandName="Edit" CommandArgument='<%#Eval("LangId") %>'

                            ImageUrl="~/Image/edit.jpg" />



                <asp:TemplateField HeaderText="Delete">


                        <asp:ImageButton ID="imgBtnDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("LangId")%>'

                            ImageUrl="~/Image/delete_icon.gif" OnClientClick="return confirm('Do you want to continue ?')" />




            <PagerSettings Mode="Numeric" />


 protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)


        DBDataContext dc = new DBDataContext();

        if (e.CommandName == "Edit")


           int rowIndex = int.Parse(e.CommandArgument.ToString());

           string LangName = grdList.DataKeys[rowIndex]["LangName"].ToString();                             



Sumit Kesarwani

Total Post:378

Posted on    September-01-2014 12:48 AM

Hi Jeet, 

You are assigning LangId and assuming it as row index in command argument. If id is greater then number of rows then you should get out of range index




CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'

Don't want to miss updates? Please click the below button!

Follow MindStick