从datagridview中选择多行并显示ID

时间:2016-05-15 14:38:19

标签: vb.net datagridview access

我正在做一个简单的测验计划。

以下是我的程序的工作原理:用户可以从两个选项中进行选择;

  • 要么只选择一个类别(DropDownList)
  • 选择多个类别(从DataGridView中选择多行)

现在我只想要如果用户选择多行,则所选行的所有ID都必须显示在MsgBox()中。无论如何我能做到吗?提前谢谢。

被修改

好的,到目前为止,我有这个:

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

我计划在数组中保护所选行的id,然后显示它。但问题是,当我选择2

时,我在Element中只获得一个id

3 个答案:

答案 0 :(得分:0)

您可以遍历所有选定的datagridview行:

For Each selectedItem As DataGridViewRow In DataGridView1.SelectedRows

要获取您喜欢的ID或任何值,请查看: DataGridView get column values

答案 1 :(得分:0)

(代表OP发布)

感谢您的帮助@cyril

#!/usr/bin/python

import re

try:
    fname = raw_input("Enter file name: ")
    fh = open(fname)
except:
    print 'Invalid Input'
    quit()

numlist = list()

for line in fh:
    nums = re.findall('[0-9]+',line)
    if len(nums) < 1 : continue
    num = int(nums[0])
    numlist.append(num)

print (numlist)

答案 2 :(得分:0)

要显示ID并在datagridview中选择“多行”,我认为这对您有所帮助。

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

我认为,如果我输入错误,请纠正我