2

我无法弄清楚如何过滤组合框以仅显示包含迄今为止输入的文本的项目。

这是构建组合框绑定到的数据表的函数:

Function users() As DataTable
        Dim ds As New DataSet
        Dim dt As DataTable
        Dim dr As DataRow
        Dim idCoulumn As DataColumn
        Dim nameCoulumn As DataColumn

        dt = New DataTable()
        idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))
        nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))

        dt.Columns.Add(idCoulumn)
        dt.Columns.Add(nameCoulumn)

        dr = dt.NewRow()
        dr("ID") = 1
        dr("Name") = "Zuse, Konrad"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 2
        dr("Name") = "Torvalds, Linus"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 3
        dr("Name") = "Babbage, Charles"
        dt.Rows.Add(dr)

        ds.Tables.Add(dt)

        Return dt

    End Function

如果我键入“u”,我将如何对其进行编程以仅显示 Linus 和 Zuse 条目?

我已经尝试过这样的事情,但我真的不明白该怎么做:

Private Sub userKeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles cb_user.KeyUp
    cb_user.IsDropDownOpen = True
    cb_user.Items.Filter = Function(item)
                               Return item.ToString().Contains(cb_user.Text.Trim())
                           End Function
End Sub
4

2 回答 2

0

林克:

Dim query = (From dr As DataRow In dt.Rows Where dr("Name").ToString.Contains("Linus")).ToArray()

拉姆达:

Dim query = dt.Rows.AsEnumerable.Where(Function(dr) dr("Name").ToString.Contains("Linus")).ToArray()
于 2012-11-12T00:44:35.420 回答
0

请尝试此控件https://www.nuget.org/packages/THEFilteredComboBox/我认为在您的情况下就足够了

于 2015-07-27T23:18:35.243 回答