I have a DataGridView
control in my Windows Forms application that allows users to edit product listing. To edit the product category, I want user to add new entries or select from the ones already entered before. To achieve this I added a comboBox
column that is binded to a DataSource
that gets the distinct category names from the products table. With the help of some other SO questions I was able make this comboBox
editable using this code:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == CategorySelector.Index)
{
ComboBox combo = e.Control as ComboBox;
if (combo == null)
return;
combo.DropDownStyle = ComboBoxStyle.DropDown;
}
}
But the problem is that when I try to edit the category comboBox
column and add new category other than the listed, and when I switch to other cell, it switches back to old category item for existing product or blank for new product. Please tell me how can I add new category through this comboBox
column?