1

我有一个DataGridView包含DataGridViewColumn一个按钮和一个按钮。当我单击按钮时,我想检查是否选中了 datagridview 中的所有复选框。

我使用以下代码,但它不起作用:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    For i As Integer = 0 To DataGridView1.Rows.Count - 1
        Dim CheckBox As DataGridViewCheckBoxCell = DirectCast(DataGridView1.Rows(i).Cells(0), DataGridViewCheckBoxCell)
        If Not CheckBox.Value = Not CheckBox.Value Then
            MsgBox("True")
        End If
    Next
End Sub
4

2 回答 2

3

我认为您的 IF 语句有问题。它应该检查是否Value = True而不是.value = Not Checkbox,Value

If CheckBox.Value = True Then
   MsgBox("True")
End If
于 2012-07-04T21:17:08.510 回答
0

我真的不知道你的逻辑应该用这条线做什么:

If Not CheckBox.Value = Not CheckBox.Value Then

看起来你在说,“如果不是价值 = 不是价值”???

尝试这个:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  For i As Integer = 0 To DataGridView1.Rows.Count - 1
    'Dim CheckBox As DataGridViewCheckBoxCell = DirectCast(DataGridView1.Rows(i).Cells(0), DataGridViewCheckBoxCell)
    'If Not CheckBox.Value = Not CheckBox.Value Then
    '  MsgBox("True")
    'End If
    Dim obj As Object = DataGridView1.Rows(i).Cells(0)
    If (Not obj Is Nothing) Then
      Dim checkBox1 As DataGridViewCheckBoxCell = DirectCast(obj, DataGridViewCheckBoxCell)
      Dim objValue As Object = checkBox1.Value
      If (Not objValue Is Nothing) Then
        Dim checked As Boolean = DirectCast(objValue, Boolean)
        If (checked) Then
          MsgBox("True")
        End If
      End If
    End If
  Next
End Sub
于 2012-07-04T21:20:00.680 回答