ForEverLearning

Hi,

How can you add a ComboBox to a bound DataGridView

I know how to add a ComboBox if the DataGridView is NOT bound using something like:

cbo = new DataGridViewComboBoxCell();

and then populating the ComboBox, construct other row columns and finally perfrom a

dataGridViewx.Rows.Add(dataGridRow);

This does not work if the DataGrid is bound - Hence my question.

Thanks for your help.



Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

Figo Fei - MSFT

Hi,

If you want to add combo box column to datagridview, you can do like this:

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
dataGridView1.Columns.Add(col);

If you have further problems, pls feel free to let me know. Thanks






Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

ForEverLearning

Hi,

Thanks for your help.

Your suggestion above adds a new column of listboxes. This works if the DataGridView is empty. But if you have the DataGridView already bound to a data source, this approach does not help.

May be I did not put the question clearly - what need to be able to do is:

1-Bind the DataGridView to a data source

2-Allow the user to go to a cell and use a ComboBox at specific cells - The ComboBox should show the bound data value and provide alternatives. For example if you are displaying employee roles, the cell should show the current employee role and allow the end user to list and select a different role from the ComboBox

Regards,





Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

Figo Fei - MSFT

Hi,

You are able to add a new column, even if the DataGridView is bound to a specific datasource.(Set the DataGridView's GenerateMember true)

However since the DataGridView is bound, you can't add a single cell to a bound row.

According to you description, you don't need to create a single cell as comboBox, what about putting a comboBox outside the DataGridView or adding a comboBox column into the Grid.

Thanks






Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

ForEverLearning

Hi,

Thanks again for your help.

As per your suggestions, I can't add the ComboBox outside the DataGridView, because, the ComboBox is used to control a specific column value selection while the user is editing a specific row.

As per your second suggestion "Adding a ComboBox into the Grid" - This is exactly what I am trying to do. But since the DataGridView is bound, I don't know how to do that...





Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

Figo Fei - MSFT

Hi,

So, what exact you need, could you describe it in details

P.S. To add a ComboBoxColumn into or ouside seems to be feasible anyway, just as I mentioned before.

Thank you






Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

ForEverLearning

Hi,

Thanks again for your interest in this thread ... The questions is as follows:

Say we have a table called State. Each row has 2 columns - StateID and StateName.

You want a DataGridView to be bound to this table and have the user enter the StateID and select from ComboBox a corresponding StateName.

The list of state names is filled programatically in the code (in any way).

How can we achieve this

Thanks.





Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

Figo Fei - MSFT

Hi,

Add a DataGridViewComboBoxColumn to datagridview:

            DataGridViewComboBoxColumn state = new DataGridViewComboBoxColumn();
            state.DataSource = ...; //dataTable
            state.DataPropertyName = "StateName";
            state.ValueMember = "StateName";
            state.DisplayMember = state.ValueMember;
            dataGridView1.Columns.Add(state);

 






Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

ForEverLearning

Hi,

That worked fine for me. Thanks for your help.





Re: Windows Forms Data Controls and Databinding Adding a ComboBox to a bound DataGridView

Darren Gulliver

Hi,

First of all I apologise for opening up a thread that¡¯s a year old but this is the only topic where I am trying to do the same thing as "ForEverLearning" but I'm having difficulties.

I bind my DataGridView to the datasource in the usual way:

Code Snippet
VB.NET Code
DataGridView1.DataSource = oDataSet.Tables("Items")

I then want to make one of my column's as a ComboBoxColumn, but when I allow this bit of code run

Code Snippet

VB.Net Code

Dim cbm As New DataGridViewComboBoxColumn

cbm.DataSource = oDataSet.Tables("Programs") '<-- List of Exe's in my Programs Table which is to populate the ComboBoxColumn List Items

cbm.DataPropertyName = "Executable"

cbm.ValueMember = "Executable"

cbm.DisplayMember = cbm.ValueMember

cbm.HeaderText = "ItemID"

DataGridView1.Columns.RemoveAt(5) '<-- Column "ItemID" from the "Items" table

DataGridView1.Columns.Insert(5, cbm) '<-- Insert "Item ID" back to orginal position but as ComboBoxColumn

the List Items have populated correcly but the cell values have gone.

Also once my cbm.DataSource is set I want to Insert a Blank value at the top of the list, cbm.Items.Insert(0,""), but VB states I cannot add items that has already been bound.

Any Idea's where I am going wrong.

Thanks for your help.

Darren