vb.net 如何按行而不是按列计算选中的数据网格视图复选框的总数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23728412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:33:33  来源:igfitidea点击:

how to count the total number of checked data grid view check boxes by rows not by columns

vb.netloopsdatagridviewdatagridviewcolumndatagridviewrow

提问by Juri14

We are currently doing a student monitoring attendance , and we want to count the total number of days each student is present and absent .

我们目前正在做一个学生监控出勤,我们想计算每个学生在场和缺勤的总天数。

     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

"This is what we wanted to do but instead of counting horizontally it counts vertically.Is there anyone who can help us solving this problem?

“这就是我们想要做的,但不是横向计数,而是纵向计数。有没有人可以帮助我们解决这个问题?

采纳答案by GoroundoVipa

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

Try This ....

尝试这个 ....

回答by GoroundoVipa

I Think this is what you want...

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

enter image description here

在此处输入图片说明

This Code Actually Check every Cell that contains Checkbox Or 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

回答by GoroundoVipa

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

Leave the Attendance Column Blank cause the Total will be insert there...

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

Wew, I Think it solves your Problem =D

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

回答by GoroundoVipa

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

Okay Here it is all done...

好了,到这里就全部搞定了……