无法使用命令名称从gridview中删除行

时间:2013-12-07 09:32:15

标签: asp.net c#-4.0 gridview

很多天我一直试图通过命令名删除gridview中的行,但是我无法做到这一点。

在gridview row命令中,我添加了一些代码中显示的东西,以删除gridview行,那么如何删除该行呢?

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("connectionstring1");
    string str;
    str = "select * from Products INNER JOIN orders on Products.picID=orders.productID";
    SqlCommand cmd = new SqlCommand(str, con);
    con.Open();

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("picID", typeof(int)));
    dt.Columns.Add(new DataColumn("title", typeof(string)));
    dt.Columns.Add(new DataColumn("price", typeof(int)));
    dt.Columns.Add(new DataColumn("dateAdded", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("picURL", typeof(string)));
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {

        DataRow dr = dt.NewRow();
        dr["picID"] = Convert.ToInt32(reader["picID"]);
        dr["title"] = reader["title"];
        dr["price"] = Convert.ToInt32(reader["price"]);
        dr["dateAdded"] = reader["dateAdded"];
        dr["picURL"] = ResolveUrl("~/images/" + reader["picURL"]);
        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }

}

protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{


    if (e.CommandName == "removeRow")
    {
        string pic = Convert.ToString(e.CommandArgument);
        deleteCart(pic);       
    }

}


private void deleteCart(string orderID)

{

    SqlConnection con = new SqlConnection("connectionstring1");
    string str;
   str = "delete from orders where orderID='" + orderID + "'";

    con.Open();
    SqlCommand cmd = new SqlCommand(str, con);
    SqlDataReader dr = cmd.ExecuteReader();
    con.Close();

}

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" ForeColor="#333333" GridLines="None"  OnRowCommand="Gridview1_RowCommand" >
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="picID" HeaderText="pic id" />
            <asp:BoundField DataField="title" HeaderText="title" />
            <asp:BoundField DataField="price" HeaderText="price" />
            <asp:BoundField DataField="dateAdded" HeaderText="date" />
            <asp:ImageField DataImageUrlField="picURL" HeaderText="image">
            </asp:ImageField>
            <asp:ButtonField ButtonType="Button" Text="remove" CommandName="removeRow" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

1 个答案:

答案 0 :(得分:0)

改进版本的代码

对于Add/ Update/ Delete,您应该使用cmd.ExecuteNonQuery()代替cmd.ExecuteReader();

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
     girdivewBind();
  }
}

  protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
     if (e.CommandName == "removeRow")
     {
        string pic = Convert.ToString(e.CommandArgument);
        deleteCart(pic);   
        girdivewBind(); // Bind your gridview again.    
      }
  }


public void deleteCart(string orderID)
{
  using (SqlConnection con = new SqlConnection(cn.ConnectionString))
   {
     using (SqlCommand cmd = new SqlCommand())
     {
        cmd.CommandText = "delete from orders where orderID=@id";
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", orderID);
        con.Open();
        var temp = cmd.ExecuteNonQuery();
        con.Close();
       }
   }
 }

 public void girdivewBind()
 { 
   SqlConnection con = new SqlConnection("connectionstring1");
   string str = "select * from Products INNER JOIN orders on Products.picID=orders.productID";
   SqlCommand cmd = new SqlCommand(str, con);
   con.Open();
   DataTable dt = new DataTable();
   dt.Columns.Add(new DataColumn("picID", typeof(int)));
   dt.Columns.Add(new DataColumn("title", typeof(string)));
   dt.Columns.Add(new DataColumn("price", typeof(int)));
   dt.Columns.Add(new DataColumn("dateAdded", typeof(DateTime)));
   dt.Columns.Add(new DataColumn("picURL", typeof(string)));
   SqlDataReader reader = cmd.ExecuteReader();
   while (reader.Read())
     {
        DataRow dr = dt.NewRow();
        dr["picID"] = Convert.ToInt32(reader["picID"]);
        dr["title"] = reader["title"];
        dr["price"] = Convert.ToInt32(reader["price"]);
        dr["dateAdded"] = reader["dateAdded"];
        dr["picURL"] = ResolveUrl("~/images/" + reader["picURL"]);
        dt.Rows.Add(dr);
       } reader.Close();
         con.Close();
       GridView1.DataSource = dt;
       GridView1.DataBind();
     }
  }