vb.net 从所选行中选择单个单元格数据 - DataGridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10599960/
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-09-09 16:06:25  来源:igfitidea点击:

Selecting single cell data from selected row - DataGridView

vb.net

提问by HarryBeasant

I have a datagridview with data in it, I have set it so you can only select the row and one at a time.

我有一个包含数据的 datagridview,我已经设置了它,因此您一次只能选择一行。

However, I need to get the data from one of the cells in the selected row, I have tried this;

但是,我需要从所选行中的一个单元格中获取数据,我已经试过了;

Dim data As String = DataGridView1.SelectedRows.Cells(5).toString

Something like that, but I can't get the value of it, does anyone know the way to do it?

类似的东西,但我无法获得它的价值,有人知道怎么做吗?

回答by Steve

Perhaps the right syntax is:

也许正确的语法是:

Dim data As String = DataGridView1.SelectedRows(0).Cells(5).Value.ToString 

See MSDN docs

请参阅MSDN 文档

回答by alwayslearn

Dim data As String = YourDataGrid.Item("YourColumnHere", YourDataGrid.CurrentRow.Index).Value

hope this help!

希望这有帮助!

回答by Leopold Stotch

SelectedRows requires an index parameter. If you've set it to only select one row, then it will always be 0. Try changing that line to:

SelectedRows 需要一个索引参数。如果您将其设置为仅选择一行,则它将始终为 0。尝试将该行更改为:

Dim data As String = DataGridView1.SelectedRows(0).Cells(5).Value.toString()

回答by BINU NARAYANAN NELLIYAMPATHI

'hope this code is also another way............ DataGridView1.SelectedRows(DataGridView1.CurrentRow.Index).Cells(5).Value.ToString()

'希望这段代码也是另一种方式........ DataGridView1.SelectedRows(DataGridView1.CurrentRow.Index).Cells(5).Value.ToString()

回答by Trebuchet

Rather than use ordinal numbers to identify which row you have selected, if this call is being used inside an event that is related to the datagridview, there should be a value passed to the event called "e" which is a structure of event arguments. if e.RowIndex is valid for that eventarg structure, you can use it as the value of the row selected:

如果在与 datagridview 相关的事件中使用此调用,而不是使用序数来标识您选择了哪一行,则应该有一个值传递给名为“e”的事件,它是一个事件参数结构。如果 e.RowIndex 对该 eventarg 结构有效,则可以将其用作所选行的值:

Dim strCellValue as string = DataGridView1.Rows(e.RowIndex).Cells(6).Value

Dim strCellValue as string = DataGridView1.Rows(e.RowIndex).Cells(6).Value

You can also get e.ColumnIndex from that argument.

您还可以从该参数中获取 e.ColumnIndex。