使用gridview_rowdeleting事件删除行

时间:2013-09-18 11:56:26

标签: c# asp.net ado.net

我正在尝试以编程方式删除网格视图中的一行 我创建了这个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();
        }
    }
}  

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

使用 DataKeyNames =&#34; ID&#34;

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

然后它会对你有用

答案 2 :(得分: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>