如何在gridview中查找选定的行单元格值?

时间:2012-08-01 13:51:20

标签: asp.net grid row

我正在尝试删除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。谢谢你的帮助。

1 个答案:

答案 0 :(得分: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;