如何使它工作? C#GridView删除行

时间:2018-05-08 03:44:44

标签: c# sql database gridview datagridview

protected void gv_pedidos_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int idPedido = Convert.ToInt32(gv_pedidos.DataKeys[e.RowIndex].Values["idPedido"].ToString());

    SqlConnection con = new SqlConnection(@"Server=localhost\SQLEXPRESS;Database=Kirchesch;Trusted_Connection=True;");
    SqlCommand com = new SqlCommand(@"DELETE FROM pedidosFeitos WHERE idPedido = @idPedido", con);

    com.Parameters.AddWithValue("@idPedido", idPedido);

    con.Open();

    com.ExecuteNonQuery();

    con.Close();

    preencheGrid();
}

这是我的代码的后端,我得到的错误是变量idPedido,即第三行。

2 个答案:

答案 0 :(得分:0)

只需使用Convert.ToInt32(e.RowIndex);

从所选行索引

中选择数据键进行操作

答案 1 :(得分:0)

您可以在idPedido事件中获得RowDeleting这样的内容:

int idPedido = int.Parse(gv_pedidos.Rows[e.RowIndex].FindControl("idPedido").toString());