2 Replies - 227 Views - Last Post: 05 July 2019 - 07:11 AM Rate Topic: -----

#1 Interplain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-November 16

DataGridView, Total of each type based on Pass and Fail

Posted 05 July 2019 - 04:08 AM

Hello, I am looking for some help with summing up the Total of each type based on a Pass or Fail. I have A weight Column and also a Result Column. I want to somehow show on labels the total weight of the failed and also the Total Weight of the Pass.

 Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()
        Dim cellValue As String = Nothing

        For i As Integer = 0 To DataGridView1.Rows.Count - 1

            If Not DataGridView1.Rows(i).IsNewRow Then
                cellValue = DataGridView1(3, i).Value.ToString()

                If Not dic.ContainsKey(cellValue) Then
                    dic.Add(cellValue, 1)
                Else
                    dic(cellValue) += 1
                End If
            End If
        Next

        Dim sb As StringBuilder = New StringBuilder()
        sb.AppendLine("Number of unique products:")

        For Each keyvalue As KeyValuePair(Of String, Integer) In dic
            sb.AppendLine(String.Format("{0}:  Bags {1}", keyvalue.Key, keyvalue.Value))
        Next

        LabelFailedWeight.Text = sb.ToString()
        LabelWeightF.Text = dic.Count.ToString()


I have this working that i have adapted for counting Products. How would i Adapt the code above to achieve what i want.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView, Total of each type based on Pass and Fail

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15268
  • View blog
  • Posts: 61,206
  • Joined: 12-June 08

Re: DataGridView, Total of each type based on Pass and Fail

Posted 05 July 2019 - 06:58 AM

First - you should be interacting with a dataset that backs the data grid. The grid is for display.. the dataset is for the data.

When you do that you can make your dataset have a calculatable column.

Second if you do your dataset through the designer you can see an 'expression' column to more easy do the basic math calculations for you.
Was This Post Helpful? 1
  • +
  • -

#3 Interplain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-November 16

Re: DataGridView, Total of each type based on Pass and Fail

Posted 05 July 2019 - 07:11 AM

I have managed to work out how to do it. With the code below.
    Private Sub OverallFailTotal()

        Dim PendingTotal As Integer = 0
        Dim counter As Integer
        For i = 0 To (DataGridView1.Rows.Count - 1)
            If DataGridView1.Rows(i).Cells(6).Value.ToString = "Fail" Then
                Dim temp As Integer
                Int32.TryParse(DataGridView1.Rows(i).Cells(5).Value.ToString(), temp)
                PendingTotal += temp
                counter += 1
            End If
        Next
        txtOverallFailTotal.Text = PendingTotal.ToString
    End Sub


Could be a better way to do it, but this works fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1