此代碼將執行此操作。我還沒有與一個巨大的數據表試過,所以你就要看看需要多長時間:
Private Function FindMaxDataTableValue(ByRef dt As DataTable) As Integer
Dim currentValue As Integer, maxValue As Integer
Dim dv As DataView = dt.DefaultView
For c As Integer = 0 To dt.Columns.Count - 1
dv.Sort = dt.Columns(c).ColumnName + " DESC"
currentValue = CInt(dv(0).Item(c))
If currentValue > maxValue Then maxValue = currentValue
Next
Return maxValue
End Function
這反過來排序每一列,如果第一個值大於當前最大值大它將更新它。
對於額外的信用,你可以這樣做,但有可能是一個性能命中做IndexOf
找到rowIndex
:
Private Function FindMaxDataTableValue(ByRef dt As DataTable) As Integer
Dim currentValue As Integer, maxValue As Integer
Dim rowIndex As Integer, colIndex As Integer
Dim dv As DataView = dt.DefaultView
For c As Integer = 0 To dt.Columns.Count - 1
dv.Sort = dt.Columns(c).ColumnName + " DESC"
currentValue = CInt(dv(0).Item(c))
If currentValue > maxValue Then
rowIndex = dt.Rows.IndexOf(dv(0).Row)
colIndex = c
maxValue = currentValue
End If
Next
Debug.WriteLine("Max value found at Col:" + colIndex.ToString + " Row:" + rowIndex.ToString)
Return maxValue
End Function
是你在運行程序時讀取csv文件?如果是這樣,你能在那個階段檢查最小和最大值嗎? –
@AndrewMorton,當我運行該程序時,我從csv文件加載數據(由用戶打開文件對話框選擇)並立即將其存儲到DataTable中。如果我能在那個階段檢查,我不知道如何。 – Matt
類似的問題已經在這裏解答http://stackoverflow.com/questions/7995526/return-max-value-with-linq-query-in-vbnet – nik