vb.net 从 datagridview 中选择多行并显示 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37239571/
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
select multiple rows from datagridview and show the IDs
提问by blackhorse123
I'm doing a simple quiz program.
我正在做一个简单的测验程序。
Here's how my program works: User can choose from two options;
我的程序是这样工作的: 用户可以从两个选项中进行选择;
- either choose only one category(DropDownList)
- choosing multiple category(where multiple rows are selected from the DataGridView)
- 要么只选择一个类别(DropDownList)
- 选择多个类别(从 DataGridView 中选择多行)
For now I just want that if user choose multiple rows, all the IDs of the rows selected must be shown in MsgBox(). Is there anyway that I can do it? Thank you in advance.
现在我只希望如果用户选择多行,则所选行的所有 ID 必须显示在 MsgBox() 中。无论如何我可以做到吗?先感谢您。
Edited
已编辑
Okay, so far I have this:
好的,到目前为止我有这个:
Dim id, i, j As Integer
Dim idList(1)
For Each selectedItem As DataGridViewRow In qstSets.SelectedRows
'show ids of multiple selected rows
id = qstSets.SelectedRows(0).Cells("ID").Value
idList(i) = id
i += 1
Next selectedItem
For j = 0 To 1
MsgBox("Element " & j & " = " & idList(j))
Next j
I have planned to safe the id's of selected row in array and then display it. But the problem here is that, I keep getting only one id in the Element when I have selected 2
我计划保护数组中选定行的 id,然后显示它。但这里的问题是,当我选择 2 时,我一直在元素中只得到一个 id
采纳答案by halfer
(Posted on behalf of the OP):
(代表OP发布):
Thank you for your help @cyril
感谢您的帮助@cyril
Dim id, i As Integer
Dim idList(5)
For Each selectedItem As DataGridViewRow In qstSets.SelectedRows
'show ids of multiple selected rows
id = selectedItem.Cells("ID").Value
idList(i) = id
i += 1
Next selectedItem
Dim sResult As String = ""
For Each elem As String In idList
sResult &= elem & ", "
Next
MsgBox(sResult)
回答by Cyril Iselin
You can iterate over all selected datagridview rows:
您可以遍历所有选定的 datagridview 行:
For Each selectedItem As DataGridViewRow In DataGridView1.SelectedRows
For getting the Id or any value you like, please have a look to: DataGridView get column values
要获取 Id 或您喜欢的任何值,请查看: DataGridView get column values

