Reputation: 15
I'm doing a simple quiz program.
Here's how my program works: User can choose from two options;
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
Upvotes: 1
Views: 11500
Reputation: 1
To show ID's and select Multiple rows in datagridview I think it would be helpful to You.
Dim SelectedRow as datagridview.selectedrow(0)
Dim selectedID as selectedRow.cells("ID").value
Dim Row as Datarow
Dim IDLists as List(of integer)
For i = 1 To datagridview.SelectedRows.Count()
selectedRow = datagridview.SelectedRows(i - 1)
selectedID = selectedRow.Cells(0).Value
row = _table.Select("ID =" & selectedID).FirstOrDefault()
IDlists.add(selectedID)
Next
for j = 0 to IDLists.count
messagebox.show(IDlists(j))
Next
I think This will be helpful If I am wrong correct me
Upvotes: 0
Reputation: 20440
(Posted on behalf of the OP):
Thank you for your help @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)
Upvotes: 1
Reputation: 596
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
Upvotes: 1