Playergoodi
Playergoodi

Reputation: 71

Getting the Count of rows in Datagridview that meet a certian criteria - VB.Net

I’m trying to accomplish something in my app that allows the user to see a numerical representation of certain data that is displayed in my datagridview.

So basically all the entries that have 9/9/2020, I want to show the total on a label. unfortunately, I run into a format exception that says “Input string was not in a correct format.” I think it has to do with the Date column and integer conversion. Here is my code

Dim PendingTotal As Integer = 0
Dim counter As Integer
For i = 0 To (DataGridView2.Rows.Count - 1)
    If DataGridView2.Rows(i).Cells("DataGridViewTextBoxColumn20").Value = "9/9/2020" Then
        ' PendingTotal += counter
        PendingTotal += Integer.Parse(DataGridView2.Rows(counter).Cells("DataGridViewTextBoxColumn20").Value.ToString())
    End If
Next
lblPenReview.Text = PendingTotal.ToString

If someone would be kind enough to point me in the right direction, I would greatly appreciate it.

Upvotes: 0

Views: 4902

Answers (2)

Karen Payne
Karen Payne

Reputation: 5127

Here is an example where the data source is not set and the Column type is string. Note I used one column for the demo as I am not seeing you refer to other columns for the requirements to obtain a count of cells meeting a condition.

The same can be done if the DataSource was set e.g. if set to say a DataTable we would cast the DataGridView.DataSource to a DataTable then adjust the code to work off the DataTable rather than the DataGridView.

''' <summary>
''' Get total count of rows where DataGridViewTextBoxColumn20 is
''' of type string.
''' </summary>
''' <remarks></remarks>
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView2.Rows.Add(New Object() {"9/9/2020"})
        DataGridView2.Rows.Add(New Object() {"9/8/2020"})
        DataGridView2.Rows.Add(New Object() {""})
        DataGridView2.Rows.Add(New Object() {"9/8/2020"})
        DataGridView2.Rows.Add(New Object() {"9/9/2020"})
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Value As String = "9/9/2020"
        Dim ColumnName As String = "DataGridViewTextBoxColumn20"
        lblPenReview.Text = DataGridView2 _
            .Rows.Cast(Of DataGridViewRow) _
            .Where(Function(row)
                       Return (Not IsDBNull(row.Cells(ColumnName).Value)) AndAlso (row.Cells(ColumnName).Value = Value)
                   End Function) _
            .Select(Function(row) row.Cells(ColumnName).Value).Count.ToString
    End Sub
End Class

Upvotes: 0

Steve
Steve

Reputation: 216302

It seems that one or more of values present in colum are not valid integers. This is true for empty or null values or also for decimal numbers.

If you are sure that you have only integers or null or empty values you could change your loop logic to use Int32.TryParse

If DataGridView2.Rows(i).Cells("DataGridViewTextBoxColumn20").Value = "9/9/2020" Then
    Dim temp As Integer
    Int32.TryParse(DataGridView2.Rows(i).Cells("DataGridViewTextBoxColumn20").Value.ToString(), temp)
    PendingTotal += temp
    Counter += 1
End If

Int32.TryParse will attempt to convert the passed string value in an integer. If it is successful then the second parameter (temp) will be set to the parsed value, otherwise it will be set to the integer default (0). No exception will be thrown if there is something that cannot be converted to an integer

By the way, I have changed the indexing made with the counter variable with the loop indexer. Probably you want to use counter to count the rows that match your condition

Upvotes: 1

Related Questions