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...
Printable View
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...
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
The following will return the ID value for the row which has the max value for the value column.
Place into a code module
Example call from a form with DataGridView1 exposed.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
OrCode:MsgBox(DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId"))
Both LINQ statements could be combined if you desire.Code:Dim Identifer As Long = DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId")
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
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:<Runtime.CompilerServices.Extension()>
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:<System.Diagnostics.DebuggerStepThrough()>
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.Code:MsgBox(dgvWorkFlow.GetMaxValueIdentifier("Workflow ID", "BedoeldVoor"))
http://www.vbforums.com/showpost.php...34&postcount=9
Any ways if all is resolved please mark the thread resolved.
[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
Example call from a form with DataGridView1 exposed.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
OrCode:MsgBox(DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId"))
Both LINQ statements could be combined if you desire.[/QUOTECode:Dim Identifer As Long = DataGridView1.GetMaxValueIdentifier("ColumnValue", "ColumnId")
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?
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