none
how to count datagridview row RRS feed

  • Question

  • how to count datagridview row?

    here is my code:

    For Each row As DataGridViewRow In dgv.Rows
                Dim num As Integer = 0
                For i As Integer = 0 To dgv.Columns.Count - 0
                    If dgv("Status", i).Value = "Absent" Then
                        num += 1
                    End If
                Next

                Label3.Text = num
            Next

    I want to know the number of absent 

    Wednesday, February 20, 2019 9:16 PM

Answers

  •  Maybe try using the RowCount.

    Dim count As Integer = DataGridView1.RowCount

    'Or...

            Dim count As Integer = DataGridView1.RowCount
            If DataGridView1.AllowUserToAddRows Then count -= 1


     PS - If you have the AllowUserToAddRows property set to True,  you may want to subtract 1 from the RowCount as there would be a Blank row in the DataGridView.  Updated example to show this option too.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, February 20, 2019 9:50 PM
    • Marked as answer by brylle losiloslod Wednesday, February 20, 2019 10:05 PM
    Wednesday, February 20, 2019 9:42 PM
  • Hello,

    Given there is a column name of status the following will work.

    Label1.Text = DataGridView1.
            Rows.Cast(Of DataGridViewRow).
            Where(Function(row) CStr(row.Cells("Status").Value) = "Absent").
            Count().ToString()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 20, 2019 9:56 PM
    Moderator

All replies

  •  Maybe try using the RowCount.

    Dim count As Integer = DataGridView1.RowCount

    'Or...

            Dim count As Integer = DataGridView1.RowCount
            If DataGridView1.AllowUserToAddRows Then count -= 1


     PS - If you have the AllowUserToAddRows property set to True,  you may want to subtract 1 from the RowCount as there would be a Blank row in the DataGridView.  Updated example to show this option too.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, February 20, 2019 9:50 PM
    • Marked as answer by brylle losiloslod Wednesday, February 20, 2019 10:05 PM
    Wednesday, February 20, 2019 9:42 PM
  • Hello,

    Given there is a column name of status the following will work.

    Label1.Text = DataGridView1.
            Rows.Cast(Of DataGridViewRow).
            Where(Function(row) CStr(row.Cells("Status").Value) = "Absent").
            Count().ToString()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 20, 2019 9:56 PM
    Moderator
  •  After looking at your code a little better,  I think I misunderstood what you wanted.  If you want to get the number of rows that the "Status" column is set to "Absent",  then you can also do it like this as well as what Karen has shown.  Sorry about the confusion.  8)

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim cnt As Integer = (From r As DataGridViewRow In dgv.Rows.Cast(Of DataGridViewRow) Where r.Cells.Item("Status").Value.ToString = "Absent").Count
            MessageBox.Show(cnt.ToString)
        End Sub
    


    If you say it can`t be done then i`ll try it

    Wednesday, February 20, 2019 10:14 PM