blackhorse123 blackhorse123 - 6 months ago 43
Vb.net Question

select multiple rows from datagridview and show the IDs

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)



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.

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

Answer

You can iterate over all selected datagridview rows:

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