双击时将 DataGridView 单元格值提取到变量

Dav*_*son 0 c# datagridview winforms

在我的 C# DataGridView 中,双击一行后,我希望将“ID”列单元格中的值分配给变量。我该怎么做呢??

mha*_*125 5

DataGridView 没有行双击事件。但您可以尝试单元格双击事件,如下所示:

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        string value = "";
        value = dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();
    }
Run Code Online (Sandbox Code Playgroud)

另外请注意检查e.RowIndex是否> -1,以防万一人们点击DataGridView标题,会导致异常。