Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I reference a combobox in a datagridview?

Status
Not open for further replies.

gharabed

Programmer
Sep 7, 2001
251
US
I want to set the dataSource property for a pre-defined combobox within a dataGridView. How do I reference the dataSource at runtime so I can populate the combobox values with the values I want? I have actually created a DataGridViewComboBoxColumn within my code and set the dataSource on that to my dataTable containing my combobox values, but I don't know how to assign the value of my DataGridViewComboBoxColumn to my existing combobox within my DataGridView.

So, for example, I have a dataGridView called myDGV. Say it contains two columns...The first is a dataGridViewTextBoxColumn and the second is a dataGridViewComboBoxColumn. I have a dataTable defined as follows that I want to assign

Code:
Dim dataTab as New DataTable("comboTabValues")

dataTab.Columns.Add("ID")
dataTab.Columns.Add("Name")

The data table (dataTab) contains the value (ID) and display (Name) members I want associated with my combobox. How do I assign dataTab to myDGV's combobox?

Hopefully that makes sense. I would think this is a pretty straight forward thing to do but I can't seem to find any information on how to do this.
 
Here is an example to reference the column:

Code:
        Dim cbc As DataGridViewComboBoxColumn
        cbc = Me.DataGridView2.Columns(1)
        cbc.Items.Add("Item1")
        cbc.Items.Add("Item2")
 
So then how do I set the displayMember and valueMember attributes of the combobox if I am adding objects to the combobox?
 
I see what you are saying. Here as an example:

Code:
        Dim cbo As DataGridViewComboBoxColumn = Me.DataGridView2.Columns(2)
        cbo.DataSource = dataTab 'This is the table where the possible values are defined
        cbo.DisplayMember = "ID"
        cbo.ValueMember = "Name"
        cbo.DataPropertyName = "Put The Name of the Column this corresponds to in your transactional table here"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top