C# 获取 DataGridView 中特定列的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10834844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:27:39  来源:igfitidea点击:

Get value in specific column in DataGridView

c#winformsdatagridview

提问by PJW

I have a datagridview in my Winforms Application. The user can click anywhere on the datagridview And then click a button to perform an action on that datarow However, in order to do this I need to recover the ID from that row Now bearing in mind the user probably hasnt clicked the ID column So SelectedCell.Value is no use to me here

我的 Winforms 应用程序中有一个 datagridview。用户可以单击 datagridview 上的任意位置然后单击一个按钮对该数据行执行操作但是,为了做到这一点,我需要从该行恢复 ID 现在记住用户可能还没有单击 ID 列 So SelectedCell .Value 在这里对我没用

I have tried the following code

我试过下面的代码

DataGridViewRow row = dataIPs.SelectedRows[0];
DataGridViewCell cell = row.Cells[0];
string value = cell.Value.ToString();

But this produces an Out Of Bounds error.

但这会产生越界错误。

How can I obtain the value from a specific column of the datagridview regardless of which column the user actually selects.

无论用户实际选择哪一列,如何从 datagridview 的特定列中获取值。

ANSWER

回答

The code that worked for me in the end was as follows:

最终对我有用的代码如下:

DataGridViewRow row = dataIPs.CurrentCell.OwningRow;
string value = row.Cells["IPID"].Value.ToString();

采纳答案by David Hall

You appear to be trying to access the SelectedRowscollection when no row has been selected.

您似乎SelectedRows在未选择任何行时尝试访问该集合。

The default selection mode for the DataGridViewis CellSelectand in this mode when you click on a cell no row is selected.

的默认选择模式DataGridViewCellSelect,在此模式下,当您单击单元格时,不会选择任何行。

You need to either change the selection mode or get the desired row some other way.

您需要更改选择模式或以其他方式获取所需的行。

  • You can change the selection mode to FullRowSelectwhich can be done in the designer or in code:

    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    
  • Or you can access the selected cell and the row from that:

    DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
    

    Or like this:

    DataGridViewRow row = dataGridView1.CurrentCell.OwningRow;
    
  • 您可以更改FullRowSelect可以在设计器或代码中完成的选择模式:

    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    
  • 或者您可以访问选定的单元格和其中的行:

    DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
    

    或者像这样:

    DataGridViewRow row = dataGridView1.CurrentCell.OwningRow;
    

回答by Rajesh Subramanian

Try to get the value from event args.

尝试从事件参数中获取值。

回答by Rupesh Saini

Set the SelectionMode property to FullRowSelect. This is needed for SelectedRow to work

将 SelectionMode 属性设置为 FullRowSelect。这是 SelectedRow 工作所必需的

回答by sickUnit

You can also try to add a column with buttons in your datagridview. after that you can catch an event "CellContentClick" from your datagridview, which has "RowIndex" and "ColumnIndex" properties in eventargs.

您还可以尝试在 datagridview 中添加带有按钮的列。之后,您可以从 datagridview 中捕获事件“CellContentClick”,该事件在 eventargs 中具有“RowIndex”和“ColumnIndex”属性。