1

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">
            <Columns>
                <asp:BoundField DataField="LangId" HeaderText="LangId" />
                <asp:BoundField DataField="LangName" HeaderText="LangName" />
                <asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnEdit" runat="server" CommandName="Edit" CommandArgument='<%#Eval("LangId") %>'
                            ImageUrl="~/Image/edit.jpg" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgBtnDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("LangId")%>'
                            ImageUrl="~/Image/delete_icon.gif" OnClientClick="return confirm('Do you want to continue ?')" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerSettings Mode="Numeric" />
        </asp:GridView>

 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();                              
        }                    
 }
4

4 回答 4

2

您正在将其分配LangId并假设为index命令参数中的行。如果 id 大于行数,那么您应该超出范围index

改变

CommandArgument='<%#Eval("LangId")%>'

CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'
于 2013-04-01T12:29:37.237 回答
0

你也没有设置DataKeys

添加DataKeys="LangName"到您的 GridView 定义。

于 2013-04-01T13:05:57.740 回答
0

命令参数=<%# Container.DisplayIndex %>

于 2015-07-09T06:27:55.733 回答
0

我之前遇到过同样的问题,这是我找到的解决方案。

RowCommand中,第一行必须是:

if (e.CommandName == "Page") return;
于 2017-08-04T22:52:24.853 回答