0

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:

EDIT:

      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

And here is link for control - some instructions there too : http://www.codeproject.com/Articles/8619/Flat-MultiColumn-Combobox-with-Autocomplete

LuckyLuke82
  • 586
  • 1
  • 18
  • 58
  • comment out `MtgcComboBox1.ColumnNum = 2` – Slai Aug 10 '16 at 11:12
  • nothing happens, still same as before. – LuckyLuke82 Aug 10 '16 at 11:15
  • hm .. then the only other difference I see is that the DataTable on the link code sample has name `Dim dtb As New DataTable("MyTable")` – Slai Aug 10 '16 at 11:21
  • I added this too, but still same as before. You willing to test this combobox yourself, maybe something else wrong here ? – LuckyLuke82 Aug 10 '16 at 11:25
  • no, it is from 2007 and might not work the same with newer .NET, but consider this http://stackoverflow.com/questions/15514698/any-way-for-a-combo-box-with-2-values-per-line – Slai Aug 10 '16 at 12:06
  • Ok, Thanks. Have done something simmilar to this (concatenaning string in combo), but this one uses different technique. I'll try It, all that is left for me is displaying both table fields in datagrid anyway. – LuckyLuke82 Aug 10 '16 at 12:19

2 Answers2

1

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
Community
  • 1
  • 1
Slai
  • 22,144
  • 5
  • 45
  • 53
  • I get error in line Select Join(r.ItemArray, vbTab)...:"Argument 'SourceArray' cannot be converted to type 'String'."...Thanks for this, that answer you gave me link is not so simple for me. too complicated. – LuckyLuke82 Aug 10 '16 at 12:43
  • then you can try with `Select r(0) & vbTab & r(1)` – Slai Aug 10 '16 at 12:47
  • Now It works, wow !! Exactly what I was looking for ! Just one problem though - Autocomplete gets second column to combobox, can I display first column in it ? – LuckyLuke82 Aug 10 '16 at 12:55
  • Sorry, I just needed make my combo a bit bigger :) A BIG THANKS MAN !!! – LuckyLuke82 Aug 10 '16 at 13:05
  • None of other options work, I'm getting "out of memory" error. I guess I'll have to do something like in link you provided. – LuckyLuke82 Aug 11 '16 at 08:03
0

I think you're just missing the instructions to show the columns. From the example:

 MtgcComboBox1.LoadingType = MTGCComboBox.CaricamentoCombo.DataTable
 MtgcComboBox1.SourceDataString =  {"Name", "SurName"}
 MtgcComboBox1.ColumnWidth = "100;100"
 MtgcComboBox1.SourceDataTable = dtb
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • No, see my edited question. This is how you probably meant, but with this code I don't even get 1 column to display. – LuckyLuke82 Aug 10 '16 at 10:38
  • Are you missing the ColumnWidths? (see my edit above), Also setting up columns explicitly in the datatable shouldn't be necessary. They will be created by the DataAdapter. – FloatingKiwi Aug 10 '16 at 10:57
  • Well, this is something else... With exact code I provided in edited question I still don't get anything, but with code I provided earlier you can now see 2 columns - unfortunally both columns are same - in other words ,only table column "Name" is shown. – LuckyLuke82 Aug 10 '16 at 11:04