2012-07-04 211 views
1

我有一個DataGridView其中包含一個DataGridViewColumn,也是一個按鈕。當我點擊按鈕時,我想檢查是否選中了datagridview中的所有複選框。檢查是否選中複選框DataGridViewCheckBoxCell

我用下面的代碼,但它不工作:

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 
+0

'如果不CheckBox.Value =不CheckBox.Value Then'這將始終評估真實。 –

回答

0

我真的不能告訴你的邏輯應該是這一行做:

如果不CheckBox.Value =不CheckBox.Value然後

它看起來像你說的,「如果不值=不值」???

試試這個:

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 
3

我認爲你有一個問題,您的IF語句。應該檢查是否的Value = True代替.value = Not Checkbox,Value

If CheckBox.Value = True Then 
    MsgBox("True") 
End If 
相關問題