我在我的Winforms应用程序中有一个DataGridView。
用户可以在DataGridView上的任何位置单击,然后单击按钮对该数据行执行操作。
但是,为了做到这一点,我需要从该行恢复ID。
现在请记住,用户可能没有单击ID列。
因此,SelectedCell.Value在这里对我没有用。
我尝试了以下代码:
DataGridViewRow row = dataIPs.SelectedRows[0];
DataGridViewCell cell = row.Cells[0];
string value = cell.Value.ToString();
但这会导致越界错误。
无论用户选择哪一列,我该如何获得datagridview中特定列的值。
答案
最终对我起作用的代码如下:
DataGridViewRow row = dataIPs.CurrentCell.OwningRow;
string value = row.Cells["IPID"].Value.ToString();
SelectedRows[0]
还是Cells[0]
调用时发生的? - SPFiredrake