Hello,
I am using Windows Forms.
I've created a custom DataGridView column - class is called ComboBoxColumn. When I edit cell content, the ComboBox appears in that cell. I am doing this because ComboBox is much faster than the DataGridViewComboBoxColumn
. My problem is, that I can't pass any parameters to my combo box - I would like to pass DataSource as parameter. ComboBox is in the class ComboBoxEditingControl. Is is called automatically from the class ComboBoxCell - instance of ComboBoxEditingControl is nowhere created. If I want to add my custom column to the DataGridView, I start with creating the ComboBoxColumn - everything else is automatic. I've redesigned this code from that example:
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
(it's DateTimePicker inside DataGridView). So, any idea how to pass parameters to class ComboBoxEditingControl?
Here are all my three classes:
public class ComboBoxColumn : DataGridViewColumn
{
public ComboBoxColumn() : base(new ComboBoxCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a ComboBoxCell.
if (value != null &&
!value.GetType().IsAssigna
bleFrom(ty
peof(Combo
BoxCell)))
{
throw new InvalidCastException("Must
be a ComboBoxCell");
}
base.CellTemplate = value;
}
}
}
public class ComboBoxCell : DataGridViewTextBoxCell
{
public ComboBoxCell(): base()
{
}
public override void InitializeEditingControl(i
nt rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingCont
rol(rowInd
ex, initialFormattedValue, dataGridViewCellStyle);
}
public override Type EditType
{
get
{
// Return the type of the editing control that CalendarCell uses.
return typeof(ComboBoxEditingCont
rol);
}
}
public override Type ValueType
{
get
{
return typeof(string);
}
}
}
public class ComboBoxEditingControl : ComboBox, IDataGridViewEditingContro
l
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public ComboBoxEditingControl()
{
}
public object EditingControlFormattedVal
ue
{
get
{
return this.ValueMember.ToString(
);
}
set
{
this.ValueMember = value.ToString();
}
}
public object GetEditingControlFormatted
Value(Data
GridViewDa
taErrorCon
texts context)
{
return EditingControlFormattedVal
ue;
}
public void ApplyCellStyleToEditingCon
trol(DataG
ridViewCel
lStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font
;
}
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
public bool EditingControlWantsInputKe
y(Keys keyData, bool dataGridViewWantsInputKey)
{
// Let the ComboBox handle the keys listed.
switch (keyData & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return !dataGridViewWantsInputKey
;
}
}
public void PrepareEditingControlForEd
it(bool selectAll)
{
}
public bool RepositionEditingControlOn
ValueChang
e
{
get
{
return false;
}
}
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
protected override void OnValueMemberChanged(Event
Args e)
{
valueChanged = true;
this.EditingControlDataGri
dView.Noti
fyCurrentC
ellDirty(t
rue);
base.OnValueMemberChanged(
e);
}
}