2015-12-23 40 views
0

我把一些代碼來查找我的DataGridView中檢查了什麼複選框,但由於某種原因,這是行不通的。查找行與CheckBox檢查DataGridView

我經歷中的各行的DataGridView循環:

For Each row As DataGridViewRow In dgv_assets.Rows 

Next 

然後在這裏我有鑄造的第一列作爲DataGridViewCheckBoxCell:

For Each row As DataGridViewRow In dgv_assets.Rows 

    Dim chk As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBoxCell) 

Next 

然後我檢查所有複選框即已被檢查:

For Each row As DataGridViewRow In dgv_assets.Rows 

    Dim chk As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBoxCell) 

    If chk.Value = chk.TrueValue Then 
     MessageBox.Show("Checked") 
    End If 

Next 

由於某些原因,即使複選框被選中或取消選中,他們都會彈出MessageBox。

回答

1

你的代碼幾乎是正確的,我猜鑄件的問題。

For Each row As DataGridViewRow In DataGridView1.Rows 
    Dim chk As DataGridViewCheckBoxCell = row.Cells(Column1.Name) 
    If chk.Value IsNot Nothing AndAlso chk.Value = True Then 
     MessageBox.Show("Checked: " + chk.RowIndex.ToString()) 
    End If 
Next 

Column1應該是你指的DataGridViewCheckBoxCell的列名。

+0

感謝您的回答。這工作正常。 –

+0

你應該使用'AndAlso'而不是'And',因爲它是短路的,這意味着如果左側評估爲「False」,它不會評估右側。這可以幫助防止'NullReferenceExceptions'。只是一點小費。 :) –

+0

@VisualVincent感謝您的提示,我已經改變了代碼。 –

0

你可以用這種簡單的方法做到。

For i As Integer = 0 To dtg.RowCount - 1 
     If dtg.Item(0, i).Value = "True" Then 
      MsgBox("check") 
     End If 
    Next 

我希望作爲有用

+0

哪裏解釋?爲什麼OP(或其他有相同問題的人)使用它?請參閱[如何回答](http://stackoverflow.com/help/how-to-answer)。 –

0

下面是一個使用語言的擴展方法的示例

表格電平可變

Private Const CheckBoxColName As String = "Process" 

放置在一個代碼模塊下面,而不是一個形式或類別

<System.Diagnostics.DebuggerStepThrough()> _ 
<Runtime.CompilerServices.Extension()> _ 
Public Function CheckBoxCount(ByVal GridView As DataGridView, ByVal ColumnIndex As Integer, ByVal Checked As Boolean) As Integer 
    Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count 
End Function 

使用使用上述私有變量的擴展方法,CheckBoxColName這是在DataGridView中列名的DataGridViewCheckBoxColumn

If DataGridView1.CheckBoxCount(CheckBoxColName, True) > 0 Then 
    Dim Rows = DataGridView1.GetCheckedRows1(CheckBoxColName) 
    For Each Row In Rows 
     Console.WriteLine(Row.Cells.Item(1).Value) 
    Next 
End If 

如果你想使用的列索引,而不是列名以下將這樣做

<System.Diagnostics.DebuggerStepThrough()> _ 
<Runtime.CompilerServices.Extension()> _ 
Public Function CheckBoxCount(ByVal GridView As DataGridView, ByVal ColumnIndex As Integer, ByVal Checked As Boolean) As Integer 
    Return (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where CBool(Rows.Cells(ColumnIndex).Value) = Checked).Count 
End Function 

注意兩者都允許您檢查或取消選中。

下得到的實際行

<System.Diagnostics.DebuggerStepThrough()> _ 
    <Runtime.CompilerServices.Extension()> _ 
    Public Function GetCheckedRows1(ByVal GridView As DataGridView, ByVal ColumnName As String) As List(Of DataGridViewRow) 
     Dim Temp = (From Rows In GridView.Rows.Cast(Of DataGridViewRow)() Where Not Rows.IsNewRow).ToList 
     Return (From SubRows In Temp Where CBool(SubRows.Cells(ColumnName).Value) = True).ToList 
    End Function 
0

您必須先驗證TrueValue不爲null,因爲根據文檔,缺省值爲null。然後檢查是否屬實。

以下文件:TrueValue

0

添加複選框與名稱CK1和添加的DataGridView與名稱DGRD,必須第一個單元格是DataGridViewCheckBoxCell並添加代碼:

Private Sub CK1_CheckedChanged(sender As Object, e As EventArgs) Handles CK1.CheckedChanged 
    If CK1.Checked = True Then 
     Try 
      Dim I As Integer 
      For I = 0 To Dgrd.Rows.Count - 1 
       Dim CHKRow As DataGridViewCheckBoxCell = Dgrd.Rows(I).Cells(0) 
       If CHKRow.Value = False Then 
        CHKRow.Value = True 
       End If 
      Next 
     Catch ex As Exception 
     End Try 
    Else 
     Try 
      Dim I As Integer 
      For I = 0 To Dgrd.Rows.Count - 1 
       Dim CHKRow As DataGridViewCheckBoxCell = Dgrd.Rows(I).Cells(0) 
       If CHKRow.Value = True Then 
        CHKRow.Value = False 
       End If 
      Next 
     Catch ex As Exception 
     End Try 
    End If 
End Sub