C# Placing a combo in one cell of a dataGridView bound to a dataTable

guyvaio
guyvaio used Ask the Experts™
on
Hello,

I'm searching now for some hours to achieve something that should be simple : placing a combo in one particular cell (not all cells of a column) of a dataGridView bound to a dataTable. (C# framework 2.0)

In the constructor of my form, below InitializeComponent();, I've this code :

dataGridView1.DataSource = dt;
...
DataGridViewComboBoxCell combo_langue = new DataGridViewComboBoxCell();
combo_langue.Items.Add("F");
combo_langue.Items.Add("N");
combo_langue.Items.Add("E");
combo_langue.Items.Add("D");
dataGridView1.Rows[i_langue].Cells[1] = combo_langue;

But the result is always a "classical" cell.

Thanks in advance for your help,

Best regards,

Guy
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
try setting that cell after your binding...
I'm guessing that your data binding is overriding your cell's configuration...

If you want your data to be bound to something, you'll have to bing it every time something is changing. After your binging has been set, update your cell with your combo_langue.

if you have no binding, everything will work just fine (i just tested it)

Author

Commented:
Thank you for your answer.

I'm not sure to understand your suggestion : the databinding (dataGridView1.DataSource = dt;) is already done before the setup of the cell with combo_langue.

As far as I understand, your suggestion is precisely what is already in my code.

Best regards,

Guy
Commented:
try doing this...
Add a button on a form and put this:
DataGridViewComboBoxCell combo_langue = new DataGridViewComboBoxCell();
combo_langue.Items.Add("F");
combo_langue.Items.Add("N");
combo_langue.Items.Add("E");
combo_langue.Items.Add("D");
dataGridView1.Rows[i_langue].Cells[1] = combo_langue;

in it's click event handler and see if that'll change anything.

Author

Commented:
Yes it works.
I moved this code from to constructor to form load event, and everything is now ok.
Thanks for your help,
Best regards,
Guy

Commented:
I'm glad I could help!

Cheers!

dimaj

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial