如何按行而非按列计算已检查数据网格视图复选框的总数

时间:2014-05-19 01:04:10

标签: vb.net loops datagridview datagridviewcolumn datagridviewrow

我们目前正在对学生进行监控,我们希望计算每个学生在场和缺席的总天数。

     Subject LN  FN    MI  05/21/14   05/20/14   05/21/14 05/22/14  05/23/14  P  A

     Comp101 Yu Erick   C   (checked|(unchecked)|(checked)|(checked)|(checked)|4 | 1

"这是我们想要做的事情,但不是横向计算,而是垂直计数。是否有人可以帮助我们解决这个问题?

4 个答案:

答案 0 :(得分:1)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click
Dim Present As Integer = 0
Dim Absent As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
    For b = 0 To DataGridView1.ColumnCount - 8
        If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
            Present += 1
        Else
            Absent += 1
        End If
    Next
    DataGridView1.Rows(a).Cells(10).Value =  Present 
    DataGridView1.Rows(a).Cells(11).Value =  Absent
    Present = 0
    Absent = 0
Next
End Sub

试试这个......

答案 1 :(得分:0)

我认为这就是你想要的......

enter image description here

此代码实际检查包含Checkbox或DatePresented的每个单元格

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim count As Integer = 0
    For a = 0 To DataGridView1.RowCount - 1
        For b = 0 To DataGridView1.ColumnCount - 2
            If DataGridView1.Rows(a).Cells(b + 1).Value = True Then
                count += 1
            End If
        Next
        DataGridView1.Rows(a).Cells(6).Value = count
        count = 0
    Next
End Sub

答案 2 :(得分:0)

enter image description here

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim count As Integer = 0
    For a = 0 To DataGridView1.RowCount - 1
        For b = 0 To DataGridView1.ColumnCount - 6
            If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
                count += 1
            End If
        Next
        DataGridView1.Rows(a).Cells(4).Value = count
        count = 0
    Next
End Sub

将考勤记录栏留空,因为总计将在那里插入......

Wew,我认为它解决了你的问题= D

答案 3 :(得分:0)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Present As Integer = 0
Dim Absent As Integer = 0
For a = 0 To DataGridView1.RowCount - 1
    For b = 0 To DataGridView1.ColumnCount - 6
        If DataGridView1.Rows(a).Cells(b + 5).Value = True Then
            Present += 1
        Else
            Absent += 1
        End If
    Next
    DataGridView1.Rows(a).Cells(4).Value = "Present: " & Present & " Absent: " & Absent
    Present = 0
    Absent = 0
Next
End Sub

好的,这一切都已完成......