|
-
Aug 12th, 2011, 09:10 AM
#1
Thread Starter
Fanatic Member
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...
-
Aug 12th, 2011, 09:39 AM
#2
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
-
Aug 12th, 2011, 12:50 PM
#3
Thread Starter
Fanatic Member
Re: Get max value from specific column in datagridvied
 Originally Posted by kevininstructor
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!!!!
-
Aug 12th, 2011, 01:16 PM
#4
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.
-
Aug 12th, 2011, 04:08 PM
#5
Thread Starter
Fanatic Member
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
 Originally Posted by kevininstructor
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.
-
Aug 13th, 2011, 06:55 AM
#6
Thread Starter
Fanatic Member
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?
 Originally Posted by kevininstructor
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.
-
Aug 13th, 2011, 07:01 AM
#7
Thread Starter
Fanatic Member
Re: Get max value from specific column in datagridvied
Solved it:
MsgBox(GetMaxValueIdentifier(dgvWorkFlow, "Workflow ID", "BedoeldVoor"))
Thank you very very much kevin!!!
-
Aug 13th, 2011, 07:22 AM
#8
Re: Get max value from specific column in datagridvied
 Originally Posted by bodylojohn
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.
-
May 21st, 2013, 04:59 AM
#9
Registered User
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?
-
May 21st, 2013, 03:11 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|