Search code examples
vb.netcomboboxbindingsource

combobox binding - show field names in bindingsource


I am trying to use a bindingsource as the datasource for a combobox. The display and value members of the combobox will be the field names in my bindingsource's datasource.

Currently I use a process of populating a datatable and assigning it to the datasource of the combobox. Because I already have the bindingsource populated with data it would make sense to just set the binding up rather than continue to use this code below:

  Dim dtfields As New DataTable
    dtfields = mySqlref.sqlobj.SelectData(String.Format("select column_name from information_schema.columns where table_name = '{0}' order by ordinal_position", mydata.Table), SqlLibrary.SqlLibrary.SelectType.datatable)

    cboField.DataSource = dtfields
    cboField.ValueMember = "column_name"
    cboField.DisplayMember = "column_name"

Can someone point me in the right direction? Thanks for reading.


Solution

  • The sort of answer I was looking for here was:

    if your fieldnames are already in your bindingsource then you can use linq to create an array of those fieldnames instead of using a seperate query to call the database for field names

    Here's the code I ended up using:

     Dim arraynames = (From x As DataColumn In mydata.Table.Columns Select x.ColumnName).ToArray()
        cboField.DataSource = arraynames
    

    in this example "mydata" is a dataview. It is the object upon which my binding source is created. I extract column names into an array using the datatable object inside that dataview.