Zero value problem in datagridview
I've gat a datagridview bound to a datatable and I want to show Max and Min values.
The code I have is working and showing the proper decimal Max and Min value in a textbox.
The problem I have is when there is a "0" value in the column then de value is always "0".
That is not what I want.
How can I avoid this problem?
Code:
Dim cells = From r As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)()
Where Not r.IsNewRow
Select CDec(r.Cells(3).Value)
Dim min As Decimal = cells.Min
Dim minval As New cell With {.columnIndex = 3, .rowIndex = Array.IndexOf(cells.ToArray, min)}
Dim max As Decimal = cells.Max
Dim maxval As New cell With {.columnIndex = 3, .rowIndex = Array.IndexOf(cells.ToArray, max)}
TextBox1.Text = max & " / " & min
Re: Zero value problem in datagridview
Quote:
Originally Posted by
sparrow1
That is not what I want.
So what do you want? Perhaps you could say it explicitly. Are you suggesting that negative values are being ignored when they shouldn't be or that zero values aren't being ignored when they should be? If it's the latter then you would need to add a filter to exclude zero values. It's the Where clause that does the filtering in a LINQ query and you already have one of those. You just need to add an appropriate condition to it.
Re: Zero value problem in datagridview
What I want is that a negative (Min) or positive (Max) value will be displayed.
With that information can I do other calculations and show the results in a graphic.
I found a way to exclude zero values, but still showing -12,00 or 12,00.
It maybe not the best way, but it works.
Code:
Dim Max As Decimal = 0
For Each rws As DataGridViewRow In DataGridView1.Rows
If rws.Cells(3).Value Is Nothing Or rws.Cells(3).Value = 0 Then
Else
If Max < rws.Cells(3).Value Then Max = rws.Cells(3).Value
End If
Next
Dim Min As Decimal = Max
For Each rws As DataGridViewRow In DataGridView1.Rows
If rws.Cells(3).Value Is Nothing Or rws.Cells(3).Value = 0 Then
Else
If Min > rws.Cells(3).Value Then Min = rws.Cells(3).Value
End If
Next
Re: Zero value problem in datagridview
I put your Min/Max in Function's
Code:
Option Strict On
Public Class Form1
Private dt As New DataTable()
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("ProductId", GetType(Integer)), _
New DataColumn("P1", GetType(String)), _
New DataColumn("P2", GetType(Decimal)), _
New DataColumn("P3", GetType(Decimal))})
dt.Rows.Add(1, "Apple", 45.12, 4)
dt.Rows.Add(2, "Orange", 32, 8.5)
dt.Rows.Add(3, "Cherry", 0, 5)
dt.Rows.Add(4, "Plum", -3, 0)
dt.Rows.Add(5, "Mango", 199.12, 4)
DataGridView1.DataSource = dt
Dim mMax As Decimal = GetMaxValue(dt, 2)
Dim mMin As Decimal = GetMinValue(dt, 2)
TextBox1.Text = mMax & " / " & mMin
End Sub
Public Function GetMaxValue(ByVal TB As DataTable, _
ByVal cColumn As Integer) As Decimal
Dim SumVal As Decimal = 0
For Each Rw As DataRow In TB.Rows
SumVal = CDec(TB.AsEnumerable().Max(Function(x) x(cColumn)))
Next
Return SumVal
End Function
Public Function GetMinValue(ByVal TB As DataTable, _
ByVal cColumn As Integer) As Decimal
Dim SumVal As Decimal = 0
For Each Rw As DataRow In TB.Rows
SumVal = CDec(TB.AsEnumerable().Min(Function(x) x(cColumn)))
Next
Return SumVal
End Function
End Class