I have implemented a DataGridViewComboBox
that allows editing using the code from this thread.
My problem right now is that when I focus the cell, the value is cleared. Based on the posts I've read, the item must be added to the combobox first. So here's what I have tried. Can you possibly tell me what's going wrong?
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
DataGridViewRow row = dataGridView1.CurrentRow;
DataGridViewCell cell = dataGridView1.CurrentCell;
if (cell == row.Cells[colComboBox.Name])
{
DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;
control.DropDownStyle = ComboBoxStyle.DropDown;
//For testing purposes
colComboBox.Items.Add("Test");//I'm adding the item to the combobox control
row.Cells[colComboBox.Name].Value = "Test";//Then set the value of the cell based on the item I added
}
}
}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == colComboBox.Index)
{
colComboBox.Items.Clear();
object eFV = e.FormattedValue;
if (!colComboBox.Items.Contains(eFV))
{
colComboBox.Items.Add(eFV);
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV;
}
}
}