我们如何在datagridview中循环列和行?

时间:2014-05-15 03:37:18

标签: vb.net datagridviewcolumn

让我告诉你我们想做什么,用户必须点击每个复选框,因为它代表学生出勤的次数,一旦用户点击按钮1,它将显示出现的总天数和按钮2是每个学生完全缺席。但是当我们运行程序时,它只计算每列的数量。所以请帮助我们。 。谢谢!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click
    If DataGridView1.RowCount > 1 Then
        Dim counter As Integer = 0
        Dim Counter1 As Integer = 0
        Dim Counter2 As Integer = 0
        Dim Counter3 As Integer = 0
        Dim Counter4 As Integer = 0
        Dim Counter5 As Integer = 0
        Dim Counter6 As Integer = 0
        Dim Counter7 As Integer = 0
        Dim Counter8 As Integer = 0
        Dim Counter9 As Integer = 0
        Dim Counter10 As Integer = 0




        For Each dr As DataGridViewRow In DataGridView1.Rows
            If dr.Cells("checkColumn").Value = True Then
                counter += 1

            End If
            If dr.Cells("checkCol1").Value = True Then
                Counter1 += 1
            End If
            If dr.Cells("checkCol2").Value = True Then
                Counter2 += 1
            End If
            If dr.Cells("checkCol3").Value = True Then
                Counter3 += 1
            End If
            If dr.Cells("checkCol4").Value = True Then
                Counter4 += 1
            End If
            If dr.Cells("checkCol5").Value = True Then
                Counter5 += 1
            End If
            If dr.Cells("checkCol6").Value = True Then
                Counter6 += 1
            End If
            If dr.Cells("checkCol7").Value = True Then
                Counter7 += 1
            End If
            If dr.Cells("checkCol8").Value = True Then
                Counter8 += 1
            End If
            If dr.Cells("checkCol9").Value = True Then
                Counter9 += 1
            End If
            If dr.Cells("checkCol10").Value = True Then
                Counter10 += 1
            End If





        Next
        DataGridView1.Rows(0).Cells("Present").Value = counter
        DataGridView1.Rows(1).Cells("Present").Value = Counter1
        DataGridView1.Rows(2).Cells("Present").Value = Counter2
        DataGridView1.Rows(3).Cells("Present").Value = Counter3
        DataGridView1.Rows(4).Cells("Present").Value = Counter4
        DataGridView1.Rows(5).Cells("Present").Value = Counter5
        DataGridView1.Rows(6).Cells("Present").Value = Counter6
        DataGridView1.Rows(7).Cells("Present").Value = Counter7
        DataGridView1.Rows(8).Cells("Present").Value = Counter8
        DataGridView1.Rows(9).Cells("Present").Value = Counter9


    End If
End Sub

2 个答案:

答案 0 :(得分:0)

这对你有用吗?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If DataGridView1.RowCount > 1 Then
        Dim columns As String() = { "checkColumn", "checkCol1", "checkCol2", "checkCol3", "checkCol4", "checkCol5", "checkCol6", "checkCol7", "checkCol8", "checkCol9", "checkCol10" }
        For i = 0 to columns.Length - 1
            DataGridView1.Rows(i).Cells("Present").Value = _
                DataGridView1.Rows _
                    .Cast(Of DataGridViewRow) _
                    .Select(Function (dr) dr.Cells(columns(i)).Value) _
                    .Where(Function (x) x) _
                    .Count()
        Next
    End If
End Sub

答案 1 :(得分:0)

请尝试通过DataGridView循环播放:

Dim dgv As DataGridView = YourDataGridViewControl

For r As Integer = 0 To dgv.RowCount - 1
    Dim r As DataGridViewRow = dgv.Rows(r)
    For c As Integer = 0 To dgv.ColumnCount - 1
        Dim cellValue as string = dgv.Rows(r).Cells(c).Value
    Next
Next