循环通过复选框列来计算选定的单元格

时间:2014-05-17 15:05:07

标签: vb.net datagridview datagridviewcheckboxcell

我的数据网格视图中有一个复选框列,我想限制用户只检查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事件(我的意思是,一个选中复选框。因为当我尝试运行代码时会发生什么,我检查一个复选框,然后我get MsgBox("You can only select 7 names.")弹出,因为它确实贯穿了所有行,当我使用普通的FOR_NEXT(如上所述)时,它变成了一个无限循环。)

有人可以帮我解决这个困惑吗?

1 个答案:

答案 0 :(得分:1)

假设带有列的DGV名为“colCheck”:

Private ChkCount As Integer = 0
Private Sub dgv_CellContentClick(sender As Object, 
        e As DataGridViewCellEventArgs) Handles dgv.CellContentClick

    ' check if this is from the check column
    If dgv.Columns(e.ColumnIndex).Name = "colCheck" Then 

        ' yes. so, is it checked or unchecked (user can do either)
        If CType(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, 
                  Boolean) = True Then

            ChkCount += 1          ' increment counter

            ' the "do something" goes here

            If ChkCount = 7 Then
                DoProcedureSeven()      ' do what happens when they hit 7
            End If
        Else
            ChkCount -= 1          ' if they uncheck, decrement counter

            ' be sure to UNDO something here
        End If
    End If
End Sub

您可能希望在检查某些内容时重新考虑整个副本行,因为他们也可以改变主意并取消选中。这意味着您必须找到之前复制的数据并将其删除。相反,只要等到它们达到7次检查,并立即复制所有7行(它们仍将被检查)。也许只有在ChkCount = 7时启用“Do Something”或“Finalize”按钮。

当你完成7点时发生的任何事情时,你的代码应该将ChkCount重置为0,并清除检查是否可以再次执行或重做它。