I've created a custom winForm comboBox that allows the user to add items by assigning the Text property of the comboBox then adding it to the ItemsCollection...I'm hosting this custom comboBox in a dataGridView as described in
http://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.90).aspx[
^]
I can't figure out how add items to the hosted comboBox when the user enters data into the dataGridViewCell that host the my custom comboBox.
Can someone please show me what am I missing here and what am I doing incorrectly?
Thanks in advance.
-DA
example:
public class DataGridViewComboBoxEditCell : DataGridViewTextBoxCell
{
public DataGridViewComboBoxEditCell()
:base()
{
this.Value = "";
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
ComboBoxEdit cboEdit = this.DataGridView.EditingControl as ComboBoxEdit;
if (this.Value == null)
{
cboEdit.Add((string)this.DefaultNewRowValue);
}
else
{
cboEdit.Add((string)this.Value);
}
}
public override Type EditType
{
get{return typeof(ComboBoxEdit);}
}
public override Type ValueType
{
get{ return typeof(String);}
}
public override object DefaultNewRowValue
{
get { return string.Empty; }
}
}
public class ComboBoxEdit : ComboBox, IDataGridViewEditingControl
{
DataGridView grid;
private bool valueChanged = false;
int rowIndex;
public ComboBoxEdit()
{
this.Text = "";
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDown;
}
public void Insert(string item)
{
if (item != null && item.Length > 0)
{
this.Items.Insert(0, item);
this.Text = "";
}
}
public void Add(string item)
{
if (item != null && item.Length > 0)
{
this.Items.Add(item);
this.Text = "";
}
}
protected override void OnSelectedValueChanged(EventArgs e)
{
this.Items.RemoveAt(this.SelectedIndex);
this.Text = "";
}
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && this.Text.Length > 0)
{
Add(this.Text.ToUpper());
}
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index < 0)
return;
string txt = (string)base.Items[e.Index];
e.DrawBackground();
Icon icon = Properties.Resources.delete;
e.Graphics.DrawImage(icon.ToBitmap(), e.Bounds.X, e.Bounds.Y, 15, 15);
e.Graphics.DrawString(txt, base.Font, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
e.DrawFocusRectangle();
}
public DataGridView EditingControlDataGridView
{
get { return grid; }
set { grid = value; }
}
public object EditingControlFormattedValue
{
get{return this.Text;}
set
{
this.Text = (string)value;
Add((string)value);
}
}
public int EditingControlRowIndex
{
get { return rowIndex; }
set { rowIndex = value; }
}
public bool EditingControlValueChanged
{
get { return valueChanged; }
set { valueChanged = value; }
}
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
}
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
return false;
}
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
}
public enum CtrlTypes
{
Chk,
Cbo,
Txt,
Btn
}
public partial class Form1 : Form
{
List<CtrlTypes> ct;
ComboBoxEdit cboEdit;
public Form1()
{
InitializeComponent();
grid.AllowUserToAddRows = false;
grid.RowHeadersVisible = false;
ct = new List<CtrlTypes>();
int cnt = 0;
foreach (CtrlTypes cts in Enum.GetValues(typeof(CtrlTypes)))
{
DataGridViewRow row = null;
DataGridViewCell celltype = null;
DataGridViewTextBoxCell cellname = null;
switch (cts)
{
case CtrlTypes.Btn:
row = new DataGridViewRow();
celltype = new DataGridViewButtonCell();
celltype.Value = "Btn";
cellname = new DataGridViewTextBoxCell();
cellname.Value = "Button:";
row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
break;
case CtrlTypes.Cbo:
row = new DataGridViewRow();
celltype = new DataGridViewComboBoxEditCell();
cellname = new DataGridViewTextBoxCell();
cellname.Value = "ComboBx:";
row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
break;
case CtrlTypes.Chk:
row = new DataGridViewRow();
celltype = new DataGridViewCheckBoxCell();
celltype.Value = true;
cellname = new DataGridViewTextBoxCell();
cellname.Value = "ChkBx:";
row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
break;
case CtrlTypes.Txt:
row = new DataGridViewRow();
celltype = new DataGridViewTextBoxCell();
celltype.Value = "Hello World";
cellname = new DataGridViewTextBoxCell();
cellname.Value = "TxtBx:";
row.Cells.AddRange(new DataGridViewCell[] { cellname, celltype });
break;
}
grid.Rows.Add(row);
}
}
}