扁平多柱组合框 - 填充DB表的列

时间:2016-08-10 08:40:13

标签: vb.net combobox

我找到了一个非常好的免费多列组合框,但不能用它填充第二列,到目前为止我设法只显示1列。有没有人通过Datable有这方面的经验 - 必须这样做,至少控制权的嗅觉声称如此。这是我的代码:

编辑:

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

       Dim SQL As String = "SELECT Name,Surname 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

                    MtgcComboBox1.ColumnNum = 2
                    MtgcComboBox1.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
                    MtgcComboBox1.SourceDataString = {"Name", "Surname"}
                    MtgcComboBox1.SourceDataTable = dtb

                    con.Close()

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

        End Using

这里是控制链接 - 也有一些说明: http://www.codeproject.com/Articles/8619/Flat-MultiColumn-Combobox-with-Autocomplete

2 个答案:

答案 0 :(得分:1)

这是Any way for a combo box with 2 values per line?

中答案的简化版本
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dtb As New DataTable
    Using dad As New OracleDataAdapter("SELECT Name,Surname from MyTable", "Data Source=MyDB;User Id=Lucky;Password=MyPassword;")
        dad.Fill(dtb) ' this should add the columns
    End Using

    Dim items = From r In dtb.Rows.Cast(Of DataRow) r(0).ToString & vbNullChar & r(1).ToString
    ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
    ComboBox1.DataSource = items.ToList
    'ComboBox1.DisplayMember = "Name"
    'ComboBox1.ValueMember = "Surname"
End Sub

Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
    e.DrawBackground() ' Fill the background.
    Dim items = ComboBox1.Items(e.Index).ToString.Split(ControlChars.NullChar) ' Extract the Record object corresponding to the combobox item to be drawn.
    Dim loc = e.Bounds.Location, xMid = (loc.X + e.Bounds.Width - loc.X) \ 2 ' Calculate important positions based on the area of the drop-down box.

    TextRenderer.DrawText(e.Graphics, items(0), e.Font, loc, e.ForeColor) ' Draw the first (Unique ID) string in the first half.
    TextRenderer.DrawText(e.Graphics, items(1), e.Font, New Point(xMid + 5, loc.Y), e.ForeColor) ' Draw the second (Name) string in the second half, adding a bit of padding.

    e.Graphics.DrawLine(SystemPens.ButtonFace, xMid, loc.Y, xMid, loc.Y + e.Bounds.Height) ' optional Draw the column separator line right down the middle.
    e.DrawFocusRectangle()        ' Finally, draw the focus rectangle.
End Sub

答案 1 :(得分:0)

我认为你只是错过了显示列的说明。从示例:

 MtgcComboBox1.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
 MtgcComboBox1.SourceDataString =  {"Name", "SurName"}
 MtgcComboBox1.ColumnWidth = "100;100"
 MtgcComboBox1.SourceDataTable = dtb