从实体列表中抓取特定值

时间:2014-03-04 22:28:01

标签: vb.net linq visual-studio listbox

我通过LINQ将特定列值加载到列表中,我想获取这些列的具体实例。这是我的页面加载的代码。

 Public Sub CheckRO_Load(sender As Object, e As EventArgs) Handles MyBase.Load

 Dim dc As New DataWorldDataContext.DataWorldDataContext
 Dim nonMatchingDealerID As New List(Of DataWorldDataContext.seanRFMatchTest)

    nonMatchingDealerID = (From z In dc.seanRFMatchTests Select z).ToList

    For Each item In nonMatchingDealerID

        ListBox1.Items.Add(item.ContractDealerID & " " & item.ServiceDealerID & " " &  item.intRepairFacilCode & " " & item.chrPgmCode & " " & item.chrRONum & " " & item.chvFacilityName)

    Next

End Sub

在ListBox的selectedIndexChange上,我想将item.ContractDealerID值应用于文本框。

 Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

    TextBox1.Text = ListBox1.SelectedValue.?????????????

End Sub

1 个答案:

答案 0 :(得分:1)

首先,我会考虑使用数据网格或列表视图控件。

要回答您的问题:您需要使用 SelectedItem 属性。

示例:

    Dim _SelectedValue As String = ListBox1.SelectedItem.ToString
    If Not String.IsNullOrWhiteSpace(_SelectedValue) Then
        TextBox1.Text = _SelectedValue.Substring(0, _SelectedValue.IndexOf(" "))
    Else
        TextBox1.Text = String.Empty
    End If