这段代码会做到这一点。我没有尝试过使用巨大的数据表,所以你必须看看需要多长时间:
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