Click here to Skip to main content
15,903,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
            {
                // Set the value of the editing control to the current cell value.
                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)
          {
              // Notify the DataGridView that the contents of the cell have changed.
              valueChanged = true;
              this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
              base.OnValueChanged(eventargs);
          }
Posted
Updated 23-Nov-10 2:35am
v3
Comments
Slacker007 22-Nov-10 9:25am    
Edited for readability.

1 solution

you need to convert the edit mode into normal mode by just giving the "editIndex" value as -1.

Try this after you update/delete/insert your record

gridview.EditIndex = -1;

This should be fine..


Regards,
Vamsi
 
Share this answer
 
v2
Comments
Dalek Dave 23-Nov-10 4:44am    
Good Call.
mslliviu 23-Nov-10 8:09am    
Hi, thanks for your support, but I might not define the problem corectly. I'm usign DataGridview (Windows forms) which doesn't have EditIndex Property. I will try to attach the code I'm using and a more detailed explanation of the simptoms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900