0

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
Juri14
  • 13
  • 1
  • 1
  • 8

2 Answers2

0

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
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • thanks for answering my question but there is some error in the code you gave to us . . – Juri14 May 15 '14 at 05:44
  • @Juri14 - Because your code is dependent on the UI I couldn't test it properly. Could you let me know what the error is? – Enigmativity May 15 '14 at 06:23
  • @Juri14 - I just knocked up a running test and the code worked. You're going to need to provide more detail. – Enigmativity May 15 '14 at 06:29
  • Well, this is the error: "Lambda parameter 'dr' hides a variable in an enclosing block, a previously defined range variable, or an implicity declared variable in a query expression." – Juri14 May 16 '14 at 00:30
  • Can you please tell me what does it mean? Sorry I'm just a new in using vb.net and i'm just a student so i'm not familiar in using it. So please help us – Juri14 May 16 '14 at 00:32
  • @Juri14 - The error you're getting is because you have the variable `dr` defined twice with the same name. Try changing the line `.Select(Function (dr) dr.Cells(columns(i)).Value) _` to be `.Select(Function (dr2) dr2.Cells(columns(i)).Value) _` and it should go away. – Enigmativity May 16 '14 at 00:44
  • We change the name as you said but there is another error when we copy your code and place it into our code. "this is where we place your code and when we run the program an error show up and it said,"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"" – Juri14 May 16 '14 at 02:35
  • in this line of the code the error show up! "DataGridView1.Rows(i).Cells("Present").Value = _ DataGridView1.Rows _ .Cast(Of DataGridViewRow) _ .Select(Function(row) row.Cells(columns(i)).Value) _ .Where(Function(x) x) _ .Count()" – Juri14 May 16 '14 at 02:36
  • @Juri14 - It's most likely the `Rows(i)` part of the code, but I got that from your code. You'll need to figure it out as I can't without access to your VS project. – Enigmativity May 16 '14 at 03:24
  • @Juri14 - Just think thru the logic and I'm sure you'll figure it out. Good luck! – Enigmativity May 16 '14 at 05:04
0

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
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143