Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. DataGridViewComboBoxColumn doesn't show data until clicked in a datagridview control

DataGridViewComboBoxColumn doesn't show data until clicked in a datagridview control

Scheduled Pinned Locked Moved Visual Basic
help
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SteveNY

    Hi, I'm using vb 2005. I'm making a test project to try some things out but having problem that I have a DataGridViewComboBoxColumn binded to a child table and then adding that column to a DataGridView. The column doesn't show the data from the child table until I tripple click the column. The data is the correct data though. Details: I have three tables that are related. Parent table called Customers table with cust_id as PK Orders table with order_id and foreign key being cust_id Prices table with FK of order_id. This is the code I used to bind the DataGridViewComboBoxColumn Dim priceComboBoxColumn As New DataGridViewComboBoxColumn With priceComboBoxColumn .HeaderText = "price" .DataSource = priceBindingsource .DisplayMember = "price" .DataPropertyName = "price" .ValueMember = "price" .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing .Visible = True ' .DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox End With dgOrders.Columns.Add(priceComboBoxColumn) I have two pictures here to show what I mean http://www.matcmp.ncc.edu/~steve/vb/ Any help would be greatly appreciated. Thanks

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #2

    It's strange that you're using a Combo to show/pick the price of a product. Normally, a combo is used to pick from a standard list of items where the ID of an item in the list corresponds to an ID column in another table. For example, a list of team IDs and Names in one table and a schedule table, listing two teams ID's, instead of names. The combo would show you the team names (DisplayMember), but the value (ValueMember) of each name in the list returns that teams ID. This ID would be populated into the schedule table (DataPropertyName). For this to work you need two binding sources. One bound to the schedule data and one bound to the team data. For example, there are two BindingSources, one called MasterScheduleBindingSource, which the DataGridView is bound to is bound to a table containing the data for the schedule, and a second TeamsDataBindingSource, which will be used by the combo and is bound to a table with TeamId's and names.

    Dim comboColumn As New DataGridViewComboBoxColumn
    With comboColumn
    ' Column header that shows up in the DataGridView
    .HeaderText = "Home Team"

    ' This is the column name in the MasterScheduleBindingSource where team ID's are going to get stored.
    .DataPropertyName = "HomeTeamId"
    
    ' This is the binding for the combo to it's data
    .DataSource = TeamsDataBindingSource
    .ValueMember = "TeamID"
    .DisplayMember = "TeamName"
    

    End With
    MyDGV.Columns.Add(comboColumn)

    A guide to posting questions on CodeProject[^]
    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
         2006, 2007, 2008

    S 1 Reply Last reply
    0
    • D Dave Kreskowiak

      It's strange that you're using a Combo to show/pick the price of a product. Normally, a combo is used to pick from a standard list of items where the ID of an item in the list corresponds to an ID column in another table. For example, a list of team IDs and Names in one table and a schedule table, listing two teams ID's, instead of names. The combo would show you the team names (DisplayMember), but the value (ValueMember) of each name in the list returns that teams ID. This ID would be populated into the schedule table (DataPropertyName). For this to work you need two binding sources. One bound to the schedule data and one bound to the team data. For example, there are two BindingSources, one called MasterScheduleBindingSource, which the DataGridView is bound to is bound to a table containing the data for the schedule, and a second TeamsDataBindingSource, which will be used by the combo and is bound to a table with TeamId's and names.

      Dim comboColumn As New DataGridViewComboBoxColumn
      With comboColumn
      ' Column header that shows up in the DataGridView
      .HeaderText = "Home Team"

      ' This is the column name in the MasterScheduleBindingSource where team ID's are going to get stored.
      .DataPropertyName = "HomeTeamId"
      
      ' This is the binding for the combo to it's data
      .DataSource = TeamsDataBindingSource
      .ValueMember = "TeamID"
      .DisplayMember = "TeamName"
      

      End With
      MyDGV.Columns.Add(comboColumn)

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      S Offline
      S Offline
      SteveNY
      wrote on last edited by
      #3

      I do have two binding sources. One binding source for orders and one for prices. I used a combobox so user can select a price from available prices but I'm open to suggestions for other controls to use.

      D 1 Reply Last reply
      0
      • S SteveNY

        I do have two binding sources. One binding source for orders and one for prices. I used a combobox so user can select a price from available prices but I'm open to suggestions for other controls to use.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #4

        SteveNY wrote:

        I do have two binding sources. One binding source for orders and one for prices.

        Then you've got something else set somewhere or some other code you haven't shown us screwing this up for you.

        SteveNY wrote:

        I used a combobox so user can select a price from available prices but I'm open to suggestions for other controls to use.

        Normally, a textbox does this. You usually don't find a one-to-many relationship between products and prices.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        S 1 Reply Last reply
        0
        • D Dave Kreskowiak

          SteveNY wrote:

          I do have two binding sources. One binding source for orders and one for prices.

          Then you've got something else set somewhere or some other code you haven't shown us screwing this up for you.

          SteveNY wrote:

          I used a combobox so user can select a price from available prices but I'm open to suggestions for other controls to use.

          Normally, a textbox does this. You usually don't find a one-to-many relationship between products and prices.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          S Offline
          S Offline
          SteveNY
          wrote on last edited by
          #5

          I think you're misunderstanding my problem. It's not that the data isn't showing up. It's the fact that I have to click the column multiple times in order for the data to show up. My code to initialize the column is the same as you've posted. I made a separate demo app to show what I mean. I put a datagridview on an empty form and in the form's load event I have this code: Dim nameComboBoxColumn As New DataGridViewComboBoxColumn With nameComboBoxColumn .HeaderText = "names" .Items.Add("john doe") .Items.Add("jane doe") .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing End With DataGridView1.Columns.Add(nameComboBoxColumn) When the app loads the data is not shown in the column until I click the column.

          D 1 Reply Last reply
          0
          • S SteveNY

            I think you're misunderstanding my problem. It's not that the data isn't showing up. It's the fact that I have to click the column multiple times in order for the data to show up. My code to initialize the column is the same as you've posted. I made a separate demo app to show what I mean. I put a datagridview on an empty form and in the form's load event I have this code: Dim nameComboBoxColumn As New DataGridViewComboBoxColumn With nameComboBoxColumn .HeaderText = "names" .Items.Add("john doe") .Items.Add("jane doe") .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing End With DataGridView1.Columns.Add(nameComboBoxColumn) When the app loads the data is not shown in the column until I click the column.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #6

            SteveNY wrote:

            It's not that the data isn't showing up. It's the fact that I have to click the column multiple times in order for the data to show up.

            I understand that. Since there isn't enough information to figure out what you did wrong, I supplied the template of how to do it right.

            SteveNY wrote:

            I made a separate demo app to show what I mean. I put a datagridview on an empty form and in the form's load event I have this code: ... When the app loads the data is not shown in the column until I click the column.

            I can't duplicate the problem. There's something else you're doing that you haven't mentioned.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            S 1 Reply Last reply
            0
            • S SteveNY

              Hi, I'm using vb 2005. I'm making a test project to try some things out but having problem that I have a DataGridViewComboBoxColumn binded to a child table and then adding that column to a DataGridView. The column doesn't show the data from the child table until I tripple click the column. The data is the correct data though. Details: I have three tables that are related. Parent table called Customers table with cust_id as PK Orders table with order_id and foreign key being cust_id Prices table with FK of order_id. This is the code I used to bind the DataGridViewComboBoxColumn Dim priceComboBoxColumn As New DataGridViewComboBoxColumn With priceComboBoxColumn .HeaderText = "price" .DataSource = priceBindingsource .DisplayMember = "price" .DataPropertyName = "price" .ValueMember = "price" .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing .Visible = True ' .DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox End With dgOrders.Columns.Add(priceComboBoxColumn) I have two pictures here to show what I mean http://www.matcmp.ncc.edu/~steve/vb/ Any help would be greatly appreciated. Thanks

              J Offline
              J Offline
              javedk09
              wrote on last edited by
              #7

              try to solve the issue with the microsoft benchwork regards, Javed Ahmed Khan

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                SteveNY wrote:

                It's not that the data isn't showing up. It's the fact that I have to click the column multiple times in order for the data to show up.

                I understand that. Since there isn't enough information to figure out what you did wrong, I supplied the template of how to do it right.

                SteveNY wrote:

                I made a separate demo app to show what I mean. I put a datagridview on an empty form and in the form's load event I have this code: ... When the app loads the data is not shown in the column until I click the column.

                I can't duplicate the problem. There's something else you're doing that you haven't mentioned.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                S Offline
                S Offline
                SteveNY
                wrote on last edited by
                #8

                I haven't done anything more than what I described. I used camtasia to record the construction of a simple project from scratch. I did the same as last post, datagrid view on form, made a column, added items to column, added column to dgv. Here is the shockwave video from camtasia: http://www.matcmp.ncc.edu/~steve/vb/comboboxproblem/comboboxproblem.html[^]

                D 1 Reply Last reply
                0
                • S SteveNY

                  I haven't done anything more than what I described. I used camtasia to record the construction of a simple project from scratch. I did the same as last post, datagrid view on form, made a column, added items to column, added column to dgv. Here is the shockwave video from camtasia: http://www.matcmp.ncc.edu/~steve/vb/comboboxproblem/comboboxproblem.html[^]

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #9

                  Your example shows the expected behavior. Since the cell doesn't have a value yet (DbNull,) one isn't selected in the ComboBox.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  S 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Your example shows the expected behavior. Since the cell doesn't have a value yet (DbNull,) one isn't selected in the ComboBox.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    S Offline
                    S Offline
                    SteveNY
                    wrote on last edited by
                    #10

                    OK, So how do I have the first item selected by default? I have searched on the workaround since the dgvcmb doesn't have a selectedindex property but haven't found anything I have been able to use or haven't tried. Thanks

                    D 1 Reply Last reply
                    0
                    • S SteveNY

                      OK, So how do I have the first item selected by default? I have searched on the workaround since the dgvcmb doesn't have a selectedindex property but haven't found anything I have been able to use or haven't tried. Thanks

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #11

                      The cell has to have a value that matches a Value in the data bound to the Combo. In your example, the default cell value is DbNull, which the data in the Combo doesn't contain. Set the value of the cell to one of the values in the Combo and the combo will show the correct item.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007, 2008

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups