Results 1 to 10 of 10

Thread: Get max value from specific column in datagridvied

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Get max value from specific column in datagridvied

    Hello,

    I have a datagridview looking like this:

    ID | Value
    1 | 6
    2 | 5
    3 | 1
    4 | 12

    How gan I get the value where ID is MAX (12)?

    I tried: dgv.item(1,dgv.rowcount - 1).value

    This doesn't work.

    How can I do this?

    Thanks in advance...

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Get max value from specific column in datagridvied

    Code:
          Dim MaxValue = (From T In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                          Select CInt(T.Cells("Id").Value)).Max
    Code:
          Dim MaxValue = (From T In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                          Select CInt(T.Cells("Value").Value)).Max

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: Get max value from specific column in datagridvied

    Quote Originally Posted by kevininstructor View Post
    Code:
          Dim MaxValue = (From T In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                          Select CInt(T.Cells("Id").Value)).Max
    Code:
          Dim MaxValue = (From T In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                          Select CInt(T.Cells("Value").Value)).Max
    Thank you very much kevin...

    But I would like the value of column "value" where the id = max

    I tried to alter your code but with no succes.
    Could you please give me a push in the right direction again?

    Thanks!!!!

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Get max value from specific column in datagridvied

    The following will return the ID value for the row which has the max value for the value column.

    Place into a code module
    Code:
       ''' <summary>
       ''' 
       ''' </summary>
       ''' <param name="sender"></param>
       ''' <param name="ColumnName">Column to get max value for</param>
       ''' <param name="IdentifierColumn">Column to get id for ColumnName</param>
       ''' <returns></returns>
       ''' <remarks>
       ''' No error-handling/assertion added in the event MaxValue LINQ statement failed.
       ''' Best to check there are rows available in the DataGridView and that the ColumnName
       ''' column is numeric.
       ''' </remarks>
       <System.Diagnostics.DebuggerStepThrough()> _
       <Runtime.CompilerServices.Extension()> _
       Public Function GetMaxValueIdentifier( _
          ByVal sender As DataGridView, _
          ByVal ColumnName As String, _
          ByVal IdentifierColumn As String) As Long
    
          Dim MaxValue = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Select CLng(T.Cells(ColumnName).Value) _
             ).Max
    
          Dim Identifier = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Where T.Cells(ColumnName).Value.ToString = CStr(MaxValue) _
                Select CLng(T.Cells(IdentifierColumn).Value) _
             ).FirstOrDefault
    
          Return Identifier
    
       End Function
    Example call from a form with DataGridView1 exposed.
    Code:
    MsgBox(DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId"))
    Or

    Code:
    Dim Identifer As Long = DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId")
    Both LINQ statements could be combined if you desire.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: Get max value from specific column in datagridvied

    Thank you very much. I will try it tomorrow and will let you know.

    Thanks a bunch

    Quote Originally Posted by kevininstructor View Post
    The following will return the ID value for the row which has the max value for the value column.

    Place into a code module
    Code:
       ''' <summary>
       ''' 
       ''' </summary>
       ''' <param name="sender"></param>
       ''' <param name="ColumnName">Column to get max value for</param>
       ''' <param name="IdentifierColumn">Column to get id for ColumnName</param>
       ''' <returns></returns>
       ''' <remarks>
       ''' No error-handling/assertion added in the event MaxValue LINQ statement failed.
       ''' Best to check there are rows available in the DataGridView and that the ColumnName
       ''' column is numeric.
       ''' </remarks>
       <System.Diagnostics.DebuggerStepThrough()> _
       <Runtime.CompilerServices.Extension()> _
       Public Function GetMaxValueIdentifier( _
          ByVal sender As DataGridView, _
          ByVal ColumnName As String, _
          ByVal IdentifierColumn As String) As Long
    
          Dim MaxValue = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Select CLng(T.Cells(ColumnName).Value) _
             ).Max
    
          Dim Identifier = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Where T.Cells(ColumnName).Value.ToString = CStr(MaxValue) _
                Select CLng(T.Cells(IdentifierColumn).Value) _
             ).FirstOrDefault
    
          Return Identifier
    
       End Function
    Example call from a form with DataGridView1 exposed.
    Code:
    MsgBox(DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId"))
    Or

    Code:
    Dim Identifer As Long = DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId")
    Both LINQ statements could be combined if you desire.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: Get max value from specific column in datagridvied

    Hello Kevin,

    I tried your code and when I call the function " MsgBox(dgvWorkFlow.GetMaxValueIdentifier(1, 0))" and I get the error: "GetMaxValueIdentifier is not a member of system.forms.datagridview"

    Am Idoing something wrong?

    Quote Originally Posted by kevininstructor View Post
    The following will return the ID value for the row which has the max value for the value column.

    Place into a code module
    Code:
       ''' <summary>
       ''' 
       ''' </summary>
       ''' <param name="sender"></param>
       ''' <param name="ColumnName">Column to get max value for</param>
       ''' <param name="IdentifierColumn">Column to get id for ColumnName</param>
       ''' <returns></returns>
       ''' <remarks>
       ''' No error-handling/assertion added in the event MaxValue LINQ statement failed.
       ''' Best to check there are rows available in the DataGridView and that the ColumnName
       ''' column is numeric.
       ''' </remarks>
       <System.Diagnostics.DebuggerStepThrough()> _
       <Runtime.CompilerServices.Extension()> _
       Public Function GetMaxValueIdentifier( _
          ByVal sender As DataGridView, _
          ByVal ColumnName As String, _
          ByVal IdentifierColumn As String) As Long
    
          Dim MaxValue = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Select CLng(T.Cells(ColumnName).Value) _
             ).Max
    
          Dim Identifier = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Where T.Cells(ColumnName).Value.ToString = CStr(MaxValue) _
                Select CLng(T.Cells(IdentifierColumn).Value) _
             ).FirstOrDefault
    
          Return Identifier
    
       End Function
    Example call from a form with DataGridView1 exposed.
    Code:
    MsgBox(DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId"))
    Or

    Code:
    Dim Identifer As Long = DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId")
    Both LINQ statements could be combined if you desire.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: Get max value from specific column in datagridvied

    Solved it:

    MsgBox(GetMaxValueIdentifier(dgvWorkFlow, "Workflow ID", "BedoeldVoor"))

    Thank you very very much kevin!!!

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Get max value from specific column in datagridvied

    Quote Originally Posted by bodylojohn View Post
    Solved it:

    MsgBox(GetMaxValueIdentifier(dgvWorkFlow, "Workflow ID", "BedoeldVoor"))

    Thank you very very much kevin!!!
    Okay my guess is that you did not put GetMaxValueIdentifier into a code module with the extension tag at the top as given to you. The reason it worked as in the quote above is because language extensions without the extension tag are normal functions or procedures and even with the tag can still be called as a function or procedure.

    This tag makes the function a language extension method
    Code:
    <Runtime.CompilerServices.Extension()>
    While the following tag causes the debugger to skip code in the function if you happen to debug other code that might call this extension method.
    Code:
    <System.Diagnostics.DebuggerStepThrough()>
    Any ways lets say you have the DataGridView in Form1 with a DataGridview named dgvWorkFlow you would call it as follows if you placed my code into a code module in the same project.

    Code:
     MsgBox(dgvWorkFlow.GetMaxValueIdentifier("Workflow ID", "BedoeldVoor"))
    To see a working example of a language extension I created for the DataGridView control download the project below and study it. One of the main advangages to extensions is you can easily tell what is being done rather than looking at a procedure or function and not knowing exactly what is beings done without some thought.

    http://www.vbforums.com/showpost.php...34&postcount=9


    Any ways if all is resolved please mark the thread resolved.

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    1

    Re: Get max value from specific column in datagridvied

    [QUOTE=kevininstructor;4052378]The following will return the ID value for the row which has the max value for the value column.

    Place into a code module
    Code:
       ''' <summary>
       ''' 
       ''' </summary>
       ''' <param name="sender"></param>
       ''' <param name="ColumnName">Column to get max value for</param>
       ''' <param name="IdentifierColumn">Column to get id for ColumnName</param>
       ''' <returns></returns>
       ''' <remarks>
       ''' No error-handling/assertion added in the event MaxValue LINQ statement failed.
       ''' Best to check there are rows available in the DataGridView and that the ColumnName
       ''' column is numeric.
       ''' </remarks>
       <System.Diagnostics.DebuggerStepThrough()> _
       <Runtime.CompilerServices.Extension()> _
       Public Function GetMaxValueIdentifier( _
          ByVal sender As DataGridView, _
          ByVal ColumnName As String, _
          ByVal IdentifierColumn As String) As Long
    
          Dim MaxValue = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Select CLng(T.Cells(ColumnName).Value) _
             ).Max
    
          Dim Identifier = _
             ( _
                From T In sender.Rows.Cast(Of DataGridViewRow)() _
                Where T.Cells(ColumnName).Value.ToString = CStr(MaxValue) _
                Select CLng(T.Cells(IdentifierColumn).Value) _
             ).FirstOrDefault
    
          Return Identifier
    
       End Function
    Example call from a form with DataGridView1 exposed.
    Code:
    MsgBox(DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId"))
    Or

    Code:
    Dim Identifer As Long = DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId")
    Both LINQ statements could be combined if you desire.[/QUOTE

    First, thanx for this wonderful code.
    i've tried it, it works for Maximum Value but when i changed it for Minimum( .Min) it is not working, giving NullReferenceException , any idea why?

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Get max value from specific column in datagridvied

    Try this
    Code:
    <Runtime.CompilerServices.Extension()> _
    Public Function GetMinValueIdentifier(
        ByVal sender As DataGridView,
        ByVal ColumnName As String, ByVal IdentifierColumn As String) _
    As Long
    
        Dim MinValue =
           ( 
              From T In sender.Rows.Cast(Of DataGridViewRow)()
              Where Not T.IsNewRow
              Select CLng(T.Cells(ColumnName).Value)
           ).Min
    
        Dim Identifier =
           ( 
              From T In sender.Rows.Cast(Of DataGridViewRow)()
              Where Not T.IsNewRow AndAlso
                        T.Cells(ColumnName).Value.ToString = CStr(MinValue)
              Select CLng(T.Cells(IdentifierColumn).Value)
           ).FirstOrDefault
    
        Return Identifier
    
    End Function

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