开发者

Getting error when deleting row from GridView [closed]

开发者 https://www.devze.com 2023-03-31 15:54 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

My ASPX code is:

<asp:gridview ID="Gri开发者_开发知识库dview1" runat="server" ShowFooter="true" 
                                    AutoGenerateColumns="false" OnRowDeleting="Gridview1_RowDeleting" AutoGenerateDeleteButton="true">
        <Columns>


        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Column Name">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

     <%-- <asp:TemplateField HeaderText="Header 2">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>--%>
        <asp:TemplateField HeaderText="Data Type">
            <ItemTemplate>
                  <asp:DropDownList ID="ddldatatype" runat="server">
                  <asp:ListItem>varchar</asp:ListItem>
                  <asp:ListItem>int</asp:ListItem>
                  <asp:ListItem>numeric</asp:ListItem>
                  <asp:ListItem>uniqueidentifier</asp:ListItem>
                  <asp:ListItem>char</asp:ListItem>
                  </asp:DropDownList>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
            <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>
             <asp:Button ID="ButtonDel" runat="server" Text="Delete Row" OnClick="ButtonDel_Click" />
             <input type="hidden" runat="server" value="0" id="hiddencount" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkdelete" runat="server" >Delete</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>

and my C# code is:

 protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
     {

         Gridview1.DeleteRow((int)Gridview1.DataKeys[e.RowIndex].Value);
         Gridview1.DataBind();


     }
 if (!IsPostBack)
        {
            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }

I am getting the error:

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


the problem is the following line:

 Gridview1.DeleteRow((int)Gridview1.DataKeys[e.RowIndex].Value);

and in there the

Gridview1.DataKeys[e.RowIndex]

this cannot work, just look up the DataKeys-property and the DeleteRow-method I guess what you want to do is

Gridview1.DeleteRow(e.RowIndex)

but even here you should check the Index!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号