从Datagridview列获取最小值

时间:2013-11-30 11:17:06

标签: vb.net datagridview minimum

我想从My datagridview列中获取最低值!

我的datagridview的名称是 datagridview1

这是我的datagridview:

  

| -Number- |

     

| --- --- 90 |

     

| --- --- 70 |

     

| --- 20 --- |

     

| --- --- 80 |

     

| --- 50 --- |

     

| --- --- 60 |

我一直尝试一些代码:

Dim MinVal As Double = 99999999

For Each row As DataGridViewRow In DataGridView1.Rows

If row.Cells(0).Value < MinVal Then MinVal = row.Cells(0).Value

Next

TextBox9.Text = MinVal

没有错误。但结果是 60 20

发生了什么?请帮助我!

2 个答案:

答案 0 :(得分:2)

    Dim Max As Integer = 0

    For Each rws As DataGridViewRow In DataGridView1.Rows
        If Max < rws.Cells(0).Value Then Max = rws.Cells(0).Value
    Next

'Maximum Value
Msgbox(Max)

对于最小值,您必须使用该最大值

    Dim Min As Integer = Max

    For Each rws As DataGridViewRow In DataGridView1.Rows
        If Min > rws.Cells(0).Value Then Min = rws.Cells(0).Value
    Next

'Minimum Value
Msgbox(Min)

答案 1 :(得分:1)

尝试以下代码

Private Structure cell
    Dim rowIndex As Integer
    Dim columnIndex As Integer
End Structure
    Dim cells = From r As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                    Where Not r.IsNewRow _
                    Select CInt(r.Cells(0).Value)

        Dim min As Integer = cells.Min
Dim minval As New cell With {.columnIndex = 0, .rowIndex = Array.IndexOf(cells.ToArray, min)}

最大值---

 Dim max As Integer = cells.Max
    Dim maxAddress As New cell With {.columnIndex = 0, .rowIndex = Array.IndexOf(cells.ToArray, max)}