【发布时间】:2014-05-17 15:05:06
【问题描述】:
我的数据网格视图中有一个复选框列,我想限制用户只检查 7 个框,不多也不少。另外,我希望每次单击复选框时都会发生一些事情。像这样(仅限伪代码):
While counter is not equal to 7 {
Check if the user clicked a checkbox (say, some random checkbox 1) {
(when I say random, I mean not in order, or in no particular order)
copy row data from where the user clicked the checkbox onto another form (data 1)
increment counter to 1
display msgbox saying that the user clicked '1 out of 7'}
Check if the user clicked another random checkbox (2) {
copy row data from where the user clicked the checkbox onto another form (data 2)
increment counter to 2
display msgbox saying that the user clicked '2 out of 7'}
.
.
.
Check if the user clicked another random checkbox (7) {
copy row data from where the user clicked the checkbox onto another form (data 7)
increment counter to 7
display msgbox saying that the user clicked '7 out of 7'}
If counter is more than 7, exit sub and display msgbox('You can only select 7 names')
我已经尝试了几层和不同的 FOR_NEXT 循环安排来让它工作,但我就是不能让它工作!这是我的代码:
Private Sub dgvPaidComms_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPaidComms.CellContentClick
Dim counter As Integer = 0
For Each row As DataGridViewRow In dgvPaidComms.Rows
Dim selected As Boolean = DataGridView1.Rows(e.RowIndex).Cells(0).Value
If selected = True Then
counter += 1
End If
Next
If counter > 7 Then
MsgBox("You can only select 7 names.")
Exit Sub
End If
End Sub
但它的作用是遍历我的 DGV 中的所有行(有 50 多行),因此它显示 MsgBox("You can only select 7 names.")。
我也尝试过使用普通的 FOR-NEXT 循环(我的意思是,普通的计数器。就像 For counter As Integer = 0 to 7 ... Next 一样。)并将 For Each row As DataGridViewRow In dgvPaidComms.Rows... 放入其中,反之亦然,只是感到失望。
我迷路了。我真的很困惑。我认为它与DataGridView1.Rows(e.RowIndex).Cells(0).Value 有关,因为我认为它只捕获一个 CellClick 事件(我的意思是,一个选中的复选框。因为当我尝试运行代码时会发生什么,我选中一个复选框,然后我得到 @ 987654327@ 跳出来是因为它真的跑遍了所有的行,而当我使用普通的 FOR_NEXT(上面解释)时,它变成了一个无限循环。)
有人可以帮我解决这个困惑吗?
【问题讨论】:
标签: vb.net datagridview datagridviewcheckboxcell