Hi,
I have a custom gridview column which displays a datetimepicker control when in edit mode. I used the article from msdn
(here) in order to get to this stage. The control works fine until I connect it to a DataSource. When it is bound to a datatable, when I click in and out on the custom edit cell in the "new row" it generates a new blank row in datatable (the grid isn't displaying the new row, but he is in datatable with rowstate = added).
My question for you is, how to stop adding these new rows? What value should the cell contain when edit mode finishes so it won't be considered as data entry (I'm usign DBNull and it doesn't works, NULL is not allowed)? Or if the value isn't the problem, what method or event should I use to cancel the row creation on the end of editing that cell?
It seems I cannot add the entire code here so I will add just the methods I think that has something to do with this problem
PS: CalendatEditingControl is a custom datetimepicker (wich allows DBNull values through BindableValue) and implements IDataGridViewEditingControl
public class CalendarCell : DataGridViewTextBoxCell
{
public CalendarCell() : base()
{
this.Style.Format = "dd-MMM-yyyy";
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
if (this.RowIndex < 0 || this.ColumnIndex < 0) return;
if (this.Value == null || this.Value == DBNull.Value )
{
ctl.BindableValue = DBNull.Value;
}
else
{
ctl.BindableValue = (DateTime)this.Value;
}
}
public override void DetachEditingControl()
{
CalendarEditingControl ctl = DataGridView.EditingControl as CalendarEditingControl;
if (this.Value == DBNull.Value && ctl.BindableValue == DBNull.Value )
{
base.DetachEditingControl();
return;
}
if (this.Value != DBNull.Value && ctl.BindableValue == DBNull.Value) this.Value = DBNull.Value;
if (ctl.BindableValue != null) this.Value = ctl.BindableValue;
base.DetachEditingControl();
}
public class CalendarEditingControl : ABT_DateTimePicker , IDataGridViewEditingControl
{
....
protected override void OnValueChanged(EventArgs eventargs)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}