Combobox column to DataGridview in C# (window forms)
In many situations we need to add a combobox to our datagrid, where users can select an option.
I here show you how to add a combo box to a DataGridView control and attach datasource to it easily.
1) First insert a DataGridView control to your form
2) Then add your columns using Columns in property window(VisualStudio)
3) Click Add... button to add a combo box column
4) Give a name for your Combobox Column ! important - in my case its 'cmbStyle'
5) Ok the Dialogboxes and goto Codebehind
6) You can place the combobox databinding code in FormLoad method, because combobox will get populated for each row when a datasource is bind to the datagridview.
private void Frm_Load(object sender, EventArgs e)
{
List styles = DataAccess.GetBowlingStyles();
cmbStyle.DataSource = styles;
cmbStyle.DisplayMember = "Style";
cmbStyle.ValueMember = "ID";
}
7) Thats all. You can bind your datasource to dataGridview as usual. Now the combo box also be populated with other data.
I here show you how to add a combo box to a DataGridView control and attach datasource to it easily.
1) First insert a DataGridView control to your form
2) Then add your columns using Columns in property window(VisualStudio)
3) Click Add... button to add a combo box column
4) Give a name for your Combobox Column ! important - in my case its 'cmbStyle'
5) Ok the Dialogboxes and goto Codebehind
6) You can place the combobox databinding code in FormLoad method, because combobox will get populated for each row when a datasource is bind to the datagridview.
private void Frm_Load(object sender, EventArgs e)
{
List
cmbStyle.DataSource = styles;
cmbStyle.DisplayMember = "Style";
cmbStyle.ValueMember = "ID";
}
7) Thats all. You can bind your datasource to dataGridview as usual. Now the combo box also be populated with other data.
Comments