0

I created multiple-column drop-down list for a combobox from a DataTable, and now I want to display both columns in it too. So far only 1 column is displayed (with DisplayMember property). So basically I want Autocomplete with both columns displayed in combobox. I would be satisfied with displaying 2nd column in Textbox next to combobox too, but It must work as Autocomplete (when selected index changes, display value changes too). I need this because both Datable values (Name & Surname) will be added in another DB table together, and for user to see both values in same place.

EDITED:

   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

And drawing line between combobox columns:

 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

Any suggestions ? Thanks in advance !!

LuckyLuke82
  • 586
  • 1
  • 18
  • 58
  • I haven't used this but saw it mentioned yesterday http://www.codeproject.com/Articles/8619/Flat-MultiColumn-Combobox-with-Autocomplete – FloatingKiwi Aug 12 '16 at 08:48
  • I have tested this allready, It doesn't work properly. Beside that, my solution here works fine, I just want autocomplete for both columns. My previous thread : http://stackoverflow.com/questions/38868261/flat-multi-column-combobox-filling-columns-with-db-tables – LuckyLuke82 Aug 12 '16 at 08:54
  • haha, yeah it was you posted it. Have you tried looking at how they attempted to do the auto complete in their library? – FloatingKiwi Aug 12 '16 at 08:57
  • No, I haven't seen It. I'm just trying to get some sort of solution for my form - User needs to enter Names and Surnames that are allready in different tables (so just select them and then autofill fields or combobox with it), and save those together in different table field as string. – LuckyLuke82 Aug 12 '16 at 09:02
  • Doesn't really sound like auto complete to me. All a normal autocomplete does is make a suggestion for something it's matched in your datasource and if you hit tab then that suggestion will become the selected item of the combo box. If what i've mentioned is what you're after you could try: http://stackoverflow.com/questions/11780558/c-sharp-winforms-combobox-dynamic-autocomplete. It might give you something to get going with. – FloatingKiwi Aug 12 '16 at 09:08

2 Answers2

0

Checkout this answer: https://stackoverflow.com/a/5570901/6550457

It suggests:

 comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
 comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
Community
  • 1
  • 1
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • First of all, I'm VB.NET beginner. This Autocomplete does work, but I changed It to .Suggest only. Second of all, this solves half of problems - still only 1 column is being displayed in combobox (Name). How could I display both columns, like "John Davies" in combobox ? – LuckyLuke82 Aug 12 '16 at 09:36
  • Do you need 2 columns. If what you're after is someones full name then instead of querying for `SELECT Name,Surname from MyTable` just join them `SELECT Name + ' ' + Surname from MyTable`. Beyond that I'm not sure how you'd do it. – FloatingKiwi Aug 12 '16 at 09:39
  • And this takes us back to beginning. OK, tell me this...How would you do it... I have 1 table in DB with a lof of fields to enter data. In same table I need to enter 4 different employees in fieleds as string (tehnician, user,developer and maintainer) with names and surnames. Now I want to develop user-friendly form, where you can enter all those Names & Surnames - but those Names & Surnames should be picked from combobox/datagrid/listview etc., because they are allready in different table. How would you do that ? – LuckyLuke82 Aug 12 '16 at 09:50
  • Firstly how are your records keyed? Do you have an EmployeeId field. Or are you planning on using FirstName, SurName as a key. I'd query for the id + fullname. I'd do the autocomplete with the fullname, set the DisplayMember to the fullname and the value member to the id. Then when I need to access the other table I'd use that id. Composite keys are a bad choice if you have an alternative. – FloatingKiwi Aug 12 '16 at 10:04
  • Employees table : (ID, Name, Surname)...My table : (ID, Tehnician, User,Developer; Maintainer,,,,and all other fields)... My preferred output in my table fields would be Name & Surname together as String, because I will not use that in any queries then. But I could also use table Employee as linked table, but I don't know how to do It in VB.NET, been searching a lot. I only did that in Access, but there this things are pretty simple. – LuckyLuke82 Aug 12 '16 at 10:10
  • Good, you're on the right track. Technician, User, ... are all just Employee.Id fields. Just modify your query to `SELECT Id, Name + ' ' + Surname as FullName from MyTable`. Then set ComboBox.ValueMember to "ID" and DisplayMember to "FullName". That should get the combo box working. Then all you need to do is add a DataGridView with these ComboBox columns and you should be fine. – FloatingKiwi Aug 12 '16 at 10:17
  • See my edited question - your + ' ' + doesn't do anything. But my ||' ' || does this - when you click dropdown list, all combobox items are invisible, but once you click somewhere in list, both values are shown in combobox. And why datagrid, what did you mean by It ? – LuckyLuke82 Aug 12 '16 at 10:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120791/discussion-between-floatingkiwi-and-luckyluke82). – FloatingKiwi Aug 12 '16 at 10:33
0

Only way to achieve all is to add another combobox and bind him to same datatable. This way when you select item from combobox you see both values in them. Autocomplete works too. Doing everything in same combobox is not good, displayed text is too wide, doesn't look good.

LuckyLuke82
  • 586
  • 1
  • 18
  • 58