0

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

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

4

1 回答 1

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时才启用“做某事”或“完成”按钮。

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

于 2014-05-17T16:46:30.703 回答