简体   繁体   English

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

[英]how can we loop the column and row in the datagridview?

Let me tell you what we wanted to do, the user has to click every check box because it represents the number of the students attendance and once the user click the button 1 it will show the total number of days present and the button 2 is for the total absent of each students. 让我告诉你我们想要做什么,用户必须单击每个复选框,因为它代表了学生出勤的次数,一旦用户单击按钮1,它将显示当前的总天数,而按钮2则是每个学生的缺勤总数。 but when we run our program it only count the number per column. 但是当我们运行程序时,它仅计算每列的数量。 so please help us . 所以请帮助我们。 . thank you! 谢谢!

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

Does this work for you? 这对您有用吗?

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

Please try following for Looping through DataGridView : 请尝试以下操作以遍历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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM