none
DataGridView ComboBox bind DataSource to query

    Question

  • I have a bound DataGridView with ComboBox columns. My DB has a lookup table that combines lookups for a varity of tables/fields. I can bind the ComboBox to the llokup table in my datasources but I need to filter the items. I added a query to the TableAdapter with the WHERE clause I need but I can't seem to bind to the query, just to the entire table.

    How can I bind to the GetDataBy... query?

    I wouldn't mind loading the items at load time but I can't figure out how to get access to the bound combo box's listitems. Is it practical to load the items at form load time?

    What is the normal procedure for binding the listitems?

    Monday, November 05, 2012 9:58 PM

Answers

  • Hi Bob,

    >>Is there a way to use the query on the table adapter as the datasource?

    You can load the query to

    When you use TableAdapter query configuration wizard to create a new query, Please choose “Select which returns rows”in steps 2.

    In step 4 (choose methods to generate), please unmark the checkbox about “fill a datatable”, and mark the checkbox about “retrun a datatable”, and rename it to “GetDateBy1”. Click finish. Here is a creenshot:

    As you finish this, you can add TableAdapter to your form and use it load the result to the datatable, here is a sample:

    Dim dt As Data.DataTable = Me.AddtestTableAdapter.GetDataBy1()
    DataGridView1.DataSource = dt

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bob Heitzman Thursday, November 08, 2012 3:51 PM
    Thursday, November 08, 2012 3:55 AM
    Moderator
  • I figured out a way to populate the bound DataGridViewComboBox at load time:

                Dim dgvc As DataGridViewComboBoxColumn
                dgvc = Program_LedgerDataGridView.Columns("Status")
                dgvc.Items.Add("Board OK")
                dgvc.Items.Add("District OK")
                dgvc.Items.Add("Pending")
    You need to rename the ComboBoxColumn from the designer assigned name.
    • Marked as answer by Bob Heitzman Thursday, November 08, 2012 10:52 PM
    Thursday, November 08, 2012 10:52 PM

All replies

  • Hi bob,

    Thanks for you post.

    Here is a sample about binding the datagridview column with datatable. You can refer to this one to check how to add the bind datagridviewcombobox column in your situation:

     Dim dsventa As New DataTable
            dsventa.Columns.Add("Col1", GetType(String))
            dsventa.Rows.Add("sample")
            Dim columnaclase As New DataGridViewComboBoxColumn
            columnaclase.Name = "clase"
            columnaclase.DataPropertyName = "clase"
            columnaclase.DataSource = dsventa
            columnaclase.DisplayMember = "Col1"
            columnaclase.ValueMember = "Col1"
            Me.datagridventas.Columns.Insert(0, columnaclase)

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, November 06, 2012 10:21 AM
    Moderator
  • I'm trying to use the DataSources and bound DataGridView.

    One of the columns in the DataGridView is 'Status' for which I have a limited set of values stored in a table 'LookupValues'. LookupValues contain records for all tables and columns so I can't bind directly to the table without using a filter of some kind. I want to keep the lookup values in a table so code changes are not needed when the list items change.

    Using the DataSetDesigner I have add a query to the LookupValues table adapter that returns the filtered rows e.g. Lookup_Program_Status. The problem I'm having is there doesn't appear to be a way to bind the DataGridViewComboBox to the query - the best I can do is bind to the LookupValues table but that returns all values for all tables. Is there a way to use the query on the table adapter as the datasource?

    I'm OK with manually populating the DataGridView combo list items at run time but there doesn't appear to be a way to do that. Am I missing something?

    ----------

    I see in the DataSouce dialog that Form List Instances is availabel under Other Data Sources. I tried creating a Public list(of string) on te form but it doesn't show up in the list of choices. How do I create a list that can be used as an Other Data Source?

    Tuesday, November 06, 2012 6:17 PM
  • Hi Bob,

    >>Is there a way to use the query on the table adapter as the datasource?

    You can load the query to

    When you use TableAdapter query configuration wizard to create a new query, Please choose “Select which returns rows”in steps 2.

    In step 4 (choose methods to generate), please unmark the checkbox about “fill a datatable”, and mark the checkbox about “retrun a datatable”, and rename it to “GetDateBy1”. Click finish. Here is a creenshot:

    As you finish this, you can add TableAdapter to your form and use it load the result to the datatable, here is a sample:

    Dim dt As Data.DataTable = Me.AddtestTableAdapter.GetDataBy1()
    DataGridView1.DataSource = dt

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Bob Heitzman Thursday, November 08, 2012 3:51 PM
    Thursday, November 08, 2012 3:55 AM
    Moderator
  • I figured out a way to populate the bound DataGridViewComboBox at load time:

                Dim dgvc As DataGridViewComboBoxColumn
                dgvc = Program_LedgerDataGridView.Columns("Status")
                dgvc.Items.Add("Board OK")
                dgvc.Items.Add("District OK")
                dgvc.Items.Add("Pending")
    You need to rename the ComboBoxColumn from the designer assigned name.
    • Marked as answer by Bob Heitzman Thursday, November 08, 2012 10:52 PM
    Thursday, November 08, 2012 10:52 PM