0

我正在尝试以编程方式删除网格视图中的一行
我已经创建了这个 GridView

<asp:GridView ID="GridView1" CssClass="HeaderTables" runat="server" AllowPaging="True" 
    EmptyDataText="There is no data record to display"
    AllowSorting="True" AutoGenerateColumns="false"
    CellPadding="0" Height="0px" Width="800px"
    onpageindexchanging="GridView1_PageIndexChanging" 
    onsorting="GridView1_Sorting" onrowdeleting="GridView1_RowDeleting">
    <Columns>
        <asp:BoundField DataField="first_name" HeaderText="First name"/>
        <asp:BoundField DataField="last_name" HeaderText="Last name"/>
        <asp:BoundField DataField="mobile_phone" HeaderText="Mobile number"/>
        <asp:BoundField DataField="email" HeaderText="Email"/>
        <asp:BoundField DataField="city" HeaderText="City"/>
        <asp:BoundField DataField="street_number" HeaderText="Street number"/>

        <asp:CommandField ShowEditButton="True" ButtonType="Button" />
        <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
    </Columns>
    <HeaderStyle HorizontalAlign="Left" />
    <RowStyle HorizontalAlign="Left" />
</asp:GridView>  

我背后的代码是:

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    if (MessageBox.Show("Are you sure you want to delete this data?",
   "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {

        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["mySqlString"].ConnectionString);
        MySqlCommand cmd = new MySqlCommand("DELETE FROM persons WHERE id = @id", conn);
        MySqlParameter param = new MySqlParameter();

        try
        {
            int rowID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            cmd.Parameters.AddWithValue("@id", rowID);
            conn.Open();
            cmd.ExecuteNonQuery();
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
}  

我收到错误:
索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

4

3 回答 3

1

您的网格未设置 DataKeyNames 属性,因此该网格不跟踪任何数据键。可能这就是您收到索引错误的原因。

您应该设置 DataKeyNames 属性。在您的代码中,您还需要检查以确保集合包含元素。datakeys 集合本身可能不为空,但它可以包含零个元素。

于 2013-09-18T12:02:46.923 回答
0

您忘记将 DataKeyNames 添加到您的 gridview

DataKeyNames="Valid Column Name" //Column name here instead of Valid Column name

完整代码如下:

<asp:GridView ID="GridView1" DataKeyNames="Valid Column Name"  CssClass="HeaderTables" runat="server" AllowPaging="True" 
    EmptyDataText="There is no data record to display"
    AllowSorting="True" AutoGenerateColumns="false"
    CellPadding="0" Height="0px" Width="800px"
    onpageindexchanging="GridView1_PageIndexChanging" 
    onsorting="GridView1_Sorting" onrowdeleting="GridView1_RowDeleting">
    <Columns>
        <asp:BoundField DataField="first_name" HeaderText="First name"/>
        <asp:BoundField DataField="last_name" HeaderText="Last name"/>
        <asp:BoundField DataField="mobile_phone" HeaderText="Mobile number"/>
        <asp:BoundField DataField="email" HeaderText="Email"/>
        <asp:BoundField DataField="city" HeaderText="City"/>
        <asp:BoundField DataField="street_number" HeaderText="Street number"/>

        <asp:CommandField ShowEditButton="True" ButtonType="Button" />
        <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
    </Columns>
    <HeaderStyle HorizontalAlign="Left" />
    <RowStyle HorizontalAlign="Left" />
</asp:GridView>  
于 2013-09-18T13:53:34.450 回答
0

使用DataKeyNames="ID"

<asp:GridView ID="GridView1" CssClass="HeaderTables" runat="server" AllowPaging="True" 
    EmptyDataText="There is no data record to display" DataKeyNames="ID"
    AllowSorting="True" AutoGenerateColumns="false"
    CellPadding="0" Height="0px" Width="800px"
    onpageindexchanging="GridView1_PageIndexChanging" 
    onsorting="GridView1_Sorting" onrowdeleting="GridView1_RowDeleting">
    <Columns>
        <asp:BoundField DataField="first_name" HeaderText="First name"/>
        <asp:BoundField DataField="last_name" HeaderText="Last name"/>
        <asp:BoundField DataField="mobile_phone" HeaderText="Mobile number"/>
        <asp:BoundField DataField="email" HeaderText="Email"/>
        <asp:BoundField DataField="city" HeaderText="City"/>
        <asp:BoundField DataField="street_number" HeaderText="Street number"/>

        <asp:CommandField ShowEditButton="True" ButtonType="Button" />
        <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
    </Columns>
    <HeaderStyle HorizontalAlign="Left" />
    <RowStyle HorizontalAlign="Left" />
</asp:GridView>  

然后它会为你工作

于 2013-09-18T12:04:53.460 回答