简体   繁体   English

从gridview删除选中的行

[英]Deleting Checked rows from gridview

I'm trying to delete checked(Selected) rows from gridview , but selected row is not deleting. 我正在尝试从gridview删除选中的(选定的)行,但是选定的行并未删除。 Why? 为什么?

//Class-Method

public void DeleteDataRow(string ID)
{
    using (SqlCommand xComm = new SqlCommand())
    {
        Conn.Open();
        new SqlCommand("Delete from Costumers Where CID='" + ID + "' ", Conn);
        Conn.Close();
    }
}

//webform-Method

protected void xbutton_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GV.Rows)
    {
        CheckBox C = row.Cells[0].FindControl("Check") as CheckBox;
        string  ID= row.Cells[1].Text;
        if(C.Checked)
        {
            m.DeleteDataRow(ID);
        }
    }

    GV.DataSource = m.Select();
    GV.DataBind();
}

Are there any error messages? 是否有任何错误消息? Also where are you defining Conn , and is it accessible to DeleteDataRow() ? 另外,您在哪里定义ConnDeleteDataRow()是否可以访问它?

If so, you'll need to define xComm a bit differently within your using statement. 如果是这样,您将需要在using语句中对xComm进行一些不同的定义。

After you open the connection, you need to execute the query in some matter such as using ExecuteNonQuery to fire the sql statement. 打开连接后,您需要执行某种查询,例如使用ExecuteNonQuery触发sql语句。 See MSDN documentation 请参阅MSDN文档

public void DeleteDataRow(string ID)
{
    using (SqlCommand xComm = new SqlCommand("Delete from Customers Where CID='" + ID + "' ", Conn))
    {
        Conn.Open();
        int result = xComm.ExecuteNonQuery();
        Conn.Close();
    }
}

Another thing to note is that you may want to check the spelling of Customers in your query. 要注意的另一件事是,您可能需要检查查询中客户的拼写。

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

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