DropDownList选择大于X的最小值

时间:2012-03-08 11:52:54

标签: asp.net vb.net

我有一个DropDownList控件,我想将SelectedValue设置为下拉列表中大于0的最低值

例如我的控件drpExample具有以下值:

0
0.90
2
3.32

我想使用drpExample.SelectedValue =最大值大于0

SelectedValue应为0.90

有一种简单的方法吗?

感谢您的期待!

3 个答案:

答案 0 :(得分:2)

试试这个:

Dim lowestItem As Decimal = 0
For Each item As ListItem In ddl.Items
    Dim itemValue As Decimal = CDec(item.Value)
    If itemValue > 0 AndAlso (lowestItem = 0 Or itemValue < lowestItem) Then lowestItem = itemValue
Next
ddl.SelectedValue = lowestItem

其中ddl是您的DropDownList控件ID。

答案 1 :(得分:2)

或试试这个:

Dim L As New List(Of Decimal)
For Each item As ListItem In DropDownList.Items
    L.Add(CDec(item.Value))
Next
ddl.selectedvalue = L.Min()

答案 2 :(得分:-1)

你可以这样做:

    For Each i As ListItem In DropDownList1.Items
        If i.Value > 0 And i.Value < DropDownList1.SelectedValue Then
            DropDownList1.SelectedValue = i.Value
        End If
    Next