LuckyLuke82 LuckyLuke82 - 1 year ago 172 Question

Flat multi-column combobox - filling columns with DB tables

I found a very good free multi-column combobox, but can't fill second column with It, so far I managed to display only 1 column. Does anybody have any experience doing this via Datable - It has to be done like this, at least auhors of control claim so. Here is my code:


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;")



Using dad As New OracleDataAdapter(SQL, con)

End Using

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


Catch ex As Exception
End Try

End Using

And here is link for control - some instructions there too :

Answer Source

This is simplified version of the answer in 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download