Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

VB.Net 2005 - Adding a ComboBox to a cell of a DataGridView???

I have a DGV in which I wish to define one of the columns as a ComboBox. I know you can do this at design time, however, I wish to do it programatically. How is that done? Also, I wish to be able to bind the ComboBox to 2 columns of a Dataset (have the ComboBox show 2 values in the dropdown list) but only return 1 of the values when a user makes a selection. Is this possible? If not, how do I load the ComboBox manually?

Thanks,
Blake
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) You can't bind a ComboBox to 2 columns

2) Add column:

        Dim dgc As New DataGridViewComboBoxColumn
        dgv.Columns.Add(dgc)

3) Add items manually:

        dgc.Items.Add("Test 1")
        dgc.Items.Add("Test 2")

Bob
Avatar of BlakeMcKenna

ASKER

Here's where I get confused. The grid is already pre-defined with the number of columns as I set the grd.DataSource to the result of a SQL Select Statement (which contains 6 columns). So...with that in mind how would I assign a ComboBox to one of the columns?
You can define the column in the designer, and then programmatically add items to the drop-down.

Bob
That's what I'm not sure of...how to add items to the ComboBox programatically...
Get a reference to the DataGridViewComboBoxColumn, and call the Items.Add method as shown above.

Bob
Can you show me a code example pls?
If the name of the column is 'dgc':

Me.dgc.Items.Add("Test 1")
Me.dgc.Items.Add("Test 2")

Bob
No, I mean referencing the DataGridViewComboBoxColumn. I'm basically trying to set this code up in a SubProcedure. The column name in question is "groupID", my Grid name is grd. So, with that info, how do I create this ComboBox and then.......how do I load it from a Dataset?

    Private Sub FormatGrid()
            grd.RowHeadersVisible = False

            grd.Columns(0).Width = 80
            grd.Columns(0).HeaderText = "Action Type ID"
            grd.Columns(1).Width = 180
            grd.Columns(1).HeaderText = "Name"
            grd.Columns(2).Width = 80
            grd.Columns(2).HeaderText = "Group ID"
    End Sub

Thanks for your help!!!!
1) Pre-define the DataGridViewComboBoxColumn for the column in the designer

2) Call it 'groupID'

3) Get a reference to 'groupID'

    Me.groupID.Items.Add("Name")
    Me.groupID.Items.Add("Address")

Bob
With the code you just posted, how does it tie in with my Grid (grd)?

The line

Me.groupdID.Items.Add("Name")

appears to be the Form (Me)
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll accept your answer as it did do what it says, however, I can't get my program to do what I want it to.

Thanks for your help Bob!
What problem are you still having?

Bob