I am very new to C#, and I really am stuck. I will try to be concise.
I have some ComboBox (in the same column) in DataGridView, and I want users to be able to type in the ComboBox. I got it to semi-work…
*Problem 1:
When I type in it, even though what I typed is added to the list of selections in the drop down, the ComboBox goes blank and I have to re-select the value I just entered. How can I make it so the value I type stays as the selection and not go blank.
*Problem 2:
Is there a way I can make certain ComboBox not editable in the same column? My code seems to make all of my ComboBox user editable. How can I make some ComboBox exceptions?
Thanks in advance, so much, if you could help!
Here are the codes:
private void dm_dgview_add_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
DataGridViewComboBoxEditingControl combo = e.Control as DataGridViewComboBoxEditingControl;
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.TextChanged += new EventHandler(combo_TextChanged);
}
}
void combo_TextChanged(object sender, EventArgs e)
{
dm_dgview_add.NotifyCurrentCellDirty(true);
}
private void dm_dgview_add_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewComboBoxCell cell = dm_dgview_add.CurrentCell as DataGridViewComboBoxCell;
if (cell != null && e.ColumnIndex == dm_dgview_add.Columns[1].Index)
{
if (!cell.Items.Contains(e.FormattedValue) && e.FormattedValue != null)
{
cell.Items.Add(e.FormattedValue);
}
}
}
Please help, I greatly appreciate this!
Do you need to set cell.Items.SelectedItem = e.FormattedValue in the cell validating event?