Results 1 to 4 of 4

Thread: Zero value problem in datagridview

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Last edited by dday9; Mar 9th, 2021 at 10:19 AM. Reason: formatted code
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Zero value problem in datagridview

    Quote Originally Posted by sparrow1 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,130

    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
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width