hi
I have requirement to implement DropDown style combobox in DataGridView, Where i can Select value from Dropdown even can type new value. Here is the code that i have added for the same:
#region dgv_TETV_T_Display_EditingControlShowing
private void dgv_TETV_T_Display_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
/// This event has been added to make Topics combo editable.
///
var cbo = e.Control as ComboBox;
if (cbo == null) { return; }
this.dgv_TETV_T_Display.NotifyCurrentCellDirty(false);
cbo.DropDownStyle = ComboBoxStyle.DropDown;
cbo.TextChanged -= new EventHandler(cbo_TextChanged);
cbo.TextChanged += new EventHandler(cbo_TextChanged);
cbo.Validating -= HandleComboBoxValidating;
cbo.Validating += HandleComboBoxValidating;
}
#endregion
#region cbo_TextChanged
void cbo_TextChanged(object sender, EventArgs e)
{
this.dgv_TETV_T_Display.NotifyCurrentCellDirty(true);
}
#endregion
#region HandleComboBoxValidating
private void HandleComboBoxValidating(object sender, CancelEventArgs e)
{
var combo = sender as DataGridViewComboBoxEditingControl;
string errorMessage = "";
if (combo == null)
{ return; }
if (combo.Text.Length > 0)
{
combo.Text = combo.Text.Trim();
}
if (!combo.Items.Contains(combo.Text)) //check if item is already in drop down, if not, add it to all
{
var comboColumn = this.dgv_TETV_T_Display.Columns[this.dgv_TETV_T_Display.CurrentCell.ColumnIndex] as DataGridViewComboBoxColumn;
if ((combo.Text != "") && (Validation.IsValidGroupName(combo.Text.ToString(), ref errorMessage) == true))
{
combo.Items.Add(combo.Text);
comboColumn.Items.Add(combo.Text);
this.dgv_TETV_T_Display.CurrentCell.Value = combo.Text;
}
else
{
this.dgv_TETV_T_Display.CurrentCell.Value = "";
}
}
//else
//{
// this.dgv_TETV_T_Display.CurrentCell.Value = combo.Text;
//}
}
#endregion
With This code, whenever i type new value in comboboxcolumn, on cell validation new value gets added to dropdownlist. I want user to type new value and just want to display it , once user clicks on accept button , i want diaplyed item to add into Dropdown item list.
Please let me know, how i can achieve this.