Combobox - 显示2个数据表列

时间:2016-08-12 08:44:43

标签: vb.net combobox datatable

我从DataTable为组合框创建了多列下拉列表,现在我想在其中显示两列。到目前为止只显示1列(具有DisplayMember属性)。所以基本上我想要自动完成,两个列都显示在组合框中。我也很满意在组合框旁边的文本框中显示第二列,但它必须作为自动完成工作(当选择的索引更改时,显示值也会更改)。我需要这个,因为Datable值(Name& Surname)将一起添加到另一个DB表中,并且用户可以在同一个地方看到这两个值。

编辑:

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            Dim SQL As String = "SELECT ID,Name ||' ' || Surname as FullName from MyTable"

            Dim dtb As New DataTable()
            dtb.Columns.Add("Name", System.Type.GetType("System.String"))
            dtb.Columns.Add("Surname", System.Type.GetType("System.String"))

            Using con As OracleConnection = New OracleConnection("Data Source=MyDB;User Id=Lucky;Password=MyPassword;")

                Try

                    con.Open()

                    Using dad As New OracleDataAdapter(SQL, con)
                        dad.Fill(dtb)

                    End Using

                    Combobox1.DataSource = dtb
                    Combobox1.DisplayMember = "FullName"
                    Combobox1.ValueMember= "ID"
                    con.Close()

                Catch ex As Exception
                    'MessageBox.Show(ex.Message)
                Finally
                    con.Dispose()
                End Try

            End Using

        End Sub

在组合框列之间画线:

 Private Sub Combobox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles Combobox1.DrawItem

        e.DrawBackground()

        Dim drv As DataRowView = CType(Combobox1.Items(e.Index), DataRowView)

        Dim id As String = drv("Name").ToString()
        Dim name As String = drv("Surname").ToString()

        Dim r1 As Rectangle = e.Bounds
        r1.Width = r1.Width / 2


        Using sb As SolidBrush = New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(id, e.Font, sb, r1)
        End Using


        Using p As Pen = New Pen(Color.AliceBlue)
            e.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom)
        End Using


        Dim r2 As Rectangle = e.Bounds
        r2.X = e.Bounds.Width / 2
        r2.Width = r2.Width / 2

       Using sb As SolidBrush = New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(name, e.Font, sb, r2)
        End Using

    End Sub

有什么建议吗?在此先感谢!!

1 个答案:

答案 0 :(得分:0)

查看此答案:https://stackoverflow.com/a/5570901/6550457

它表明:

 comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
 comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;