how can we loop the column and row in the datagridview?

為{幸葍}努か 提交于 2019-12-12 01:06:20

问题


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. 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

回答1:


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



回答2:


Please try following for Looping through 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


来源:https://stackoverflow.com/questions/23668834/how-can-we-loop-the-column-and-row-in-the-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!