0

我正在将数据源加载到下拉列表并绑定,但在某些情况下,新值即将到来(区分大小写),所以我无法将下拉列表设置为选定值?如何实现这一点,意味着将数据网格选择的值显示为下拉列表中的文本?

将数据填充到下拉列表中

Me.RmtRouterName.DataSource = Me.datareader_1param("pr_list_dev_by_site", 
    SiteID, "@enter_value")
Me.RmtRouterName.DataTextField = "devname"
Me.RmtRouterName.DataValueField = "devname"

从数据网格中读取值

tmpstr = MyIIF(Me.SiteInfo1.Tables(SiteInfoTableName).Rows(0), "RmtRouterName")
If (tmpstr = String.Empty) Then
   Me.RmtRouterName.SelectedIndex = -1
Else
   Me.RmtRouterName.SelectedValue = tmpstr

在某些情况下,datareader 值和 datgrid 值不匹配(由于区分大小写)如何克服这个问题

4

1 回答 1

0

在您的Else逻辑中,在尝试设置之前尝试在下拉列表中查找文本值SelectedValue,如下所示:

辅助功能:

Public Function FindByTextCaseInsensitive(ByVal ctl As ListControl, ByVal text As String) As ListItem
    If ctl Is Nothing Then 
        Return Nothing
    End If

    For Each li As ListItem In ctl.Items
        If String.Compare(li.Text, text, True) = 0 Then 
            Return li
        End If
    Next

    Return Nothing
End Function

现在在你的Else街区,这样做:

Else
    Dim foundItem As ListItem = FindByTextCaseInsensitive(tmpstr)

    If foundItem Is Nothing Then
        Me.RmtRouterName.SelectedIndex = -1
    Else
        Me.RmtRouterName.SelectedValue = tmpstr
    End If
End If
于 2013-11-08T16:19:02.817 回答