如何从 VB.Net 中的 DataGridView 获取单元格值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19721984/
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
How to get cell value from DataGridView in VB.Net?
提问by Joseph Kim
I have a problem, how can i get value from cell of datagridview
我有一个问题,如何从 datagridview 的单元格中获取值
----------------------------------
id | p/w | post |
----------------------------------
1 | 1234 | A |
----------------------------------
2 | 4567 | S |
----------------------------------
3 | 6789 | A |
----------------------------------
I want to get 3 into the textbox,how to do that? Can anyone give some example coding? than you~
我想在文本框中输入 3,该怎么做?谁能给出一些示例编码?比你~
回答by D. Bunnell
The line would be as shown below:
该行如下所示:
Dim x As Integer
x = dgvName.Rows(yourRowIndex).Cells(yourColumnIndex).Value
回答by Leonel Maye
In you want to know the data from de selected row, you can try this snippet code:
如果你想知道 de selected row 的数据,你可以试试这个片段代码:
DataGridView1.SelectedRows.Item(0).Cells(1).Value
回答by sundar
To get the value of cell, use the following syntax,
要获取单元格的值,请使用以下语法,
datagridviewName(columnFirst, rowSecond).value
But the intellisense and MSDN documentation is wrongly saying rowFirst, colSecond
approach...
但是智能感知和 MSDN 文档错误地说明了rowFirst, colSecond
方法......
回答by Adnan Zaheer
回答by RPh_Coder
This helped me get close to what I needed and I will throw this out there for anyone else who needs it.
这帮助我接近我需要的东西,我会把它扔给任何需要它的人。
If you are looking for the value in the first cell in the selected column, you can try this. (I chose the first column, since you are asking for it to return "3", but you can change the number after Cells to get whichever column you need. Remember it is zero-based.)
如果您要查找所选列的第一个单元格中的值,您可以试试这个。(我选择了第一列,因为您要求它返回“3”,但是您可以更改 Cells 之后的数字以获得您需要的任何列。记住它是从零开始的。)
This will copy the result to the clipboard:
Clipboard.SetDataObject(Me.DataGridView1.CurrentRow.Cells(0).Value)
这会将结果复制到剪贴板:
Clipboard.SetDataObject(Me.DataGridView1.CurrentRow.Cells(0).Value)