简体   繁体   English

删除gridview行

[英]deleting row of gridview

I want to delete the row of the gridview.For this I have used the following code.Before it was working but now there is no any value in datatable. 我想删除gridview的行,为此我使用了以下代码。在它起作用之前,现在数据表中没有任何值了。

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {

        DataTable dt = GridView1.DataSource as DataTable;

        sQLcONN.Open();
        MySqlCommand objcmd = new MySqlCommand("delete   from shoppingcart where with_puja = '" + Convert.ToInt32(dt.Rows[e.RowIndex]["id"]).ToString() + "'", sQLcONN);
        objcmd.ExecuteNonQuery();
        Bindgrid();
        sQLcONN.Close();
    }
    catch (Exception ex)
    {
         Console.WriteLine("{0} Exception caught.", ex);
    }

Check for row count, if >0 delete 检查行数,如果> 0删除

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {

            DataTable dt = GridView1.DataSource as DataTable;
            if(dt.rows.count>0)
            {
            sQLcONN.Open();
            MySqlCommand objcmd = new MySqlCommand("delete   from shoppingcart where with_puja = '" + Convert.ToInt32(dt.Rows[e.RowIndex]["id"]).ToString() + "'", sQLcONN);
            objcmd.ExecuteNonQuery();
            Bindgrid();
            sQLcONN.Close();
        }
        catch (Exception ex)
        {
             Console.WriteLine("{0} Exception caught.", ex);
        }
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
    try
    {
        string id = GridView1.DataKeys[e.RowIndex].Values["with_puja"].ToString();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "delete   from shoppingcart where with_puja='" + id + "'";
        cmd.Connection = con;
        con.Open();            
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {            
        Response.Write("<script>alert('An Error occurred.</script>");
    }
    finally
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();                
        }
        Bindgrid();
    }
}

and on aspx page add DatekeyNames on it 并在aspx页面上添加DatekeyNames

 <asp:GridView ID="GridView1" runat="server" ForeColor="#333333" 
            GridLines="None" DataKeyNames="with_puja">
 </GridView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM