【问题标题】:Looping through Checkbox Columns to count selected cells循环通过复选框列来计算选定的单元格
【发布时间】: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


    【解决方案1】:

    假设列名为“colCheck”的 DGV:

    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,并清除检查他们是否可以再次执行或重做。

    【讨论】:

    • 哦,这只是 IF 语句......现在可以使用了。顺便说一句,我使用了“长格式”,因为当我为 IF 部分尝试“CType”时,它会返回错误,但是当我使用“长格式”时,它工作正常。谢谢! :)
    • 我想我在最后添加了这一点,只是为了尽量避免 HScroll 一行长(已修复,谢谢)。它不仅仅是 IF 语句。您的代码循环遍历每次点击的所有行,而不仅仅是处理这次点击。如果您在看到每张支票时复制这些内容,那么您将在目的地有大量重复。
    • 我现在明白了。我按照你的建议做了(在所有检查/取消检查之后复制数据,这样我得到的就是我需要的,或者类似的东西,但我是这样理解的,我希望我能按照你想要的方式理解它。)谢谢。
    • 我知道这个问题已经 11 天不活跃,但我仍然有关于这个主题的问题,我不想问新问题,因为它可能会导致冗余。所以 check-uncheck 事情已经工作了一段时间了,直到我注意到每当我取消选中某些行(随机,不按顺序)时,它们的值应该是空的字段(标签)是空的,因此,如果我检查第 1-5 行和未选中的第 3 和第 2 行,复制值的表单在字段 2 和 3 中有间隙,这是丑陋和错误的。我应该怎么做才能把它们整理好? (我想看 1-2-3 而不是 1- - -4-5)
    • 这听起来像是一个新问题 - 这个新问题与上述代码的作用有关数据从一个到另一个。所以我会问一个包含该代码的新问题。
    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2012-04-02
    • 2020-12-02
    • 1970-01-01
    相关资源
    最近更新 更多