1

我正在尝试删除我的 gridview 中的记录。为此,我在 gridview 中添加了一个按钮字段。然后我为按钮字段提供了一个命令名称。我写了这样的代码

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

    if (e.CommandName == "delete")
    {

        try
        {

            con.Open();
            cmd.CommandText = "Delete from Table where id='" + SelectedRowAndCellValue +"'"

            cmd.Connection = con;
            cmd.ExecuteNonQuery();

            con.Close();
            cmd.Dispose();

            GridView1.DataBind();

        }
        catch (Exception ex)
        {

            Response.Write(ex.ToString());
        }
    }
}

但我找不到 SelectedRowAndCellValue。感谢您的帮助。

4

1 回答 1

0

您可以尝试使用此代码

  int index = Convert.ToInt32(e.CommandArgument);
  GridViewRow row = GridView1.Rows[index];
  var result = row.Cells[yourIndex].Text;

  cmd.CommandText = "Delete from Table where id = "+row.Cells[yourIndex].Text; 
于 2012-08-01T13:56:55.443 回答