Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I want to bind a datagridview to datatable. three of the columns should display a combobox.

I havn't found how to change column type form textboxcolum to comboboxcolumn
google hasn't helped me. searching datagridview and comboboxcolumn brings a lot of answers but none covers my problem.

What I have tried:

on a given datatable I do :
dgv.datasource = datatable

that will make all columns to textboxcolumn
so how do I proceed form here on ?
any ideas ?
thanks in advance
Posted
Updated 9-Jan-18 11:15am

1 solution

Please, read this: How to: Bind Objects to Windows Forms DataGridView Controls | Microsoft Docs[^]

Conclusion: you can't use DataGridViewComboBoxColumn with DataTable as a DataSource of DataGridView. Take a look at below piece of code:

C#
public Form1()
{
    InitializeComponent();

    //disable column autogeneration
    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.AutoSize = true;
    //bind sample data
    dataGridView1.DataSource = CustomBindingSource();

    //create columns manually
    //TextBoxColumn
    DataGridViewTextBoxColumn txt = new DataGridViewTextBoxColumn();
    txt.DataPropertyName = "EmpId";
    txt.Name = "EmpId";
    dataGridView1.Columns.Add (txt);

    //ComboBoxColumn
    DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
    combo.DataSource = Enum.GetValues(typeof(Sex));
    combo.DataPropertyName = "Sex";
    combo.Name = "Sex";
    dataGridView1.Columns.Add(combo);

    //that's all!
}


The other interesting resources:
How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance | Microsoft Docs[^]
How to: Host Controls in Windows Forms DataGridView Cells | Microsoft Docs[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 10-Jan-18 0:09am    
5
Maciej Los 10-Jan-18 1:33am    
Thank you, Karthik.
fheyn 12-Jan-18 18:05pm    
if that's the only solution it's not applicable to my project.
the datagridview belongs to a baseform which subclasses several forms each using the dgv in a diffent way (columncount varies from 5 to 12)
using the solution shown leaves me with more work than I do have populating dgv
from each subform.
anyhow thanks for your answer
franz
Maciej Los 14-Jan-18 8:27am    
Well, i'm afraid that's the only solution...

Cheers
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900