By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

Problem in datagridview datetimepicker column.

P: 79
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have:
1) Initially all the cells of the dtp column should be blank unless they are filled by the user.
2) As soon as the user enters a cell, the dtp control should appear as the editing control of that cell. If there's a value in the cell beforehand, that value is set as the value of the dtp editing control and it is checked, else the dtp editing control remains unchecked.
3) If the user selects a date from the dtp editing control and moves to another cell the value of the dtp should be set to the previous cell in string format (In that way we can define custom format for the cell e.g. dd MMM yyyy).
4) If the user unchecks the dtp editing control while it's on a cell the value of that cell should be set to null irrespective of whether the cell had a value beforehand.

I managed everything till point 3. But can't work out with point 4. Cannot set the value of a cell to null when its dtp editing control is unchecked. There's a way to do it by handling the Datagridview_CellValidating event. But in that case if the datagridview is data bound, u need to uncheck the editing control once, goto another cell, come back to the previous one and uncheck it once again to set its cell value to null. I want to know if there's any way to do it through the derived classes or their class template that I'm using so that I wont have to use that event and everything can be done through the class template only. Please help. My code follows:

Code for the template (I'm using a code file) containing all the derived classes:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace DGV_DateTimePicker
  5. {
  6.     public class DateTimePickerColumn : DataGridViewColumn
  7.     {
  8.         public DateTimePickerColumn()
  9.             : base(new DateTimePickerCell())
  10.         {
  11.         }
  12.  
  13.         public override DataGridViewCell CellTemplate
  14.         {
  15.             get
  16.             {
  17.                 return base.CellTemplate;
  18.             }
  19.             set
  20.             {
  21.                 // Ensure that the cell used for the template is a DateTimePickerCell.
  22.                 if (value != null &&
  23.                     !value.GetType().IsAssignableFrom(typeof(DateTimePickerCell)))
  24.                 {
  25.                     throw new InvalidCastException("Must be a DateTimePickerCell");
  26.                 }
  27.                 base.CellTemplate = value;
  28.             }
  29.         }
  30.     }
  31.  
  32.     public class DateTimePickerCell : DataGridViewTextBoxCell
  33.     {
  34.  
  35.         public DateTimePickerCell()
  36.             : base()
  37.         {
  38.             // Use the custom defined date format.
  39.             this.Style.Format = "dd MMM yyyy";
  40.         }
  41.  
  42.         public override void InitializeEditingControl(int rowIndex, object
  43.             initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
  44.         {
  45.             // Set the value of the editing control to the current cell value.
  46.             base.InitializeEditingControl(rowIndex, initialFormattedValue,
  47.                 dataGridViewCellStyle);
  48.  
  49.             DateTimePickerEditingControl ctl = (DateTimePickerEditingControl)DataGridView.EditingControl;
  50.             DateTime d;
  51.             ctl.Value = DateTime.TryParse((Value ?? "").ToString(), out d) ? d : DateTime.Now;
  52.  
  53.             /*Check whether the datagridview is databound/unbound. In both cases if the value of the cell
  54.          isn't null then check the DateTimePickerEditing Control else uncheck it.*/
  55.             if (ctl.EditingControlDataGridView.CurrentCell.OwningColumn.IsDataBound)
  56.                 ctl.Checked = Value.ToString() == "" ? false : Value == null ? false : true;
  57.             else
  58.                 ctl.Checked = Value == null ? false : true;
  59.         }
  60.  
  61.         public override Type EditType
  62.         {
  63.             get
  64.             {
  65.                 // Return the type of the editing contol that DateTimePickerCell uses.
  66.                 return typeof(DateTimePickerEditingControl);
  67.             }
  68.         }
  69.  
  70.         public override Type ValueType
  71.         {
  72.             get
  73.             {
  74.                 // Return the type of the value that DateTimePickerCell contains.
  75.                 return typeof(DateTime);
  76.             }
  77.         }
  78.  
  79.         public override object DefaultNewRowValue
  80.         {
  81.             get
  82.             {
  83.                 //Return null as the default value for new row.
  84.                 return null;
  85.             }
  86.         }
  87.     }
  88.  
  89.     class DateTimePickerEditingControl : DateTimePicker, IDataGridViewEditingControl
  90.     {
  91.         DataGridView dataGridView;
  92.         private bool valueChanged = false;
  93.         int rowIndex;
  94.  
  95.         public DateTimePickerEditingControl()
  96.         {
  97.             this.Format = DateTimePickerFormat.Custom;
  98.             this.CustomFormat = "dd MMM yyyy";
  99.             this.ShowCheckBox = true;
  100.             this.Checked = false;
  101.         }
  102.  
  103.         // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
  104.         // property.
  105.         public object EditingControlFormattedValue
  106.         {
  107.             get
  108.             {
  109.                 return this.Value.ToShortDateString();
  110.             }
  111.             set
  112.             {
  113.                 if (value is String)
  114.                 {
  115.                     this.Value = DateTime.Parse((String)value);
  116.                 }
  117.             }
  118.         }
  119.  
  120.         // Implements the 
  121.         // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
  122.         public object GetEditingControlFormattedValue(
  123.             DataGridViewDataErrorContexts context)
  124.         {
  125.             return EditingControlFormattedValue;
  126.         }
  127.  
  128.         // Implements the 
  129.         // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
  130.         public void ApplyCellStyleToEditingControl(
  131.             DataGridViewCellStyle dataGridViewCellStyle)
  132.         {
  133.             this.Font = dataGridViewCellStyle.Font;
  134.             this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
  135.             this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
  136.         }
  137.  
  138.         // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
  139.         // property.
  140.         public int EditingControlRowIndex
  141.         {
  142.             get
  143.             {
  144.                 return rowIndex;
  145.             }
  146.             set
  147.             {
  148.                 rowIndex = value;
  149.             }
  150.         }
  151.  
  152.         // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
  153.         // method.
  154.         public bool EditingControlWantsInputKey(
  155.             Keys key, bool dataGridViewWantsInputKey)
  156.         {
  157.             // Let the DateTimePicker handle the keys listed.
  158.             switch (key & Keys.KeyCode)
  159.             {
  160.                 case Keys.Left:
  161.                 case Keys.Up:
  162.                 case Keys.Down:
  163.                 case Keys.Right:
  164.                 case Keys.Home:
  165.                 case Keys.End:
  166.                 case Keys.PageDown:
  167.                 case Keys.PageUp:
  168.                     return true;
  169.                 default:
  170.                     return !dataGridViewWantsInputKey;
  171.             }
  172.         }
  173.  
  174.         // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
  175.         // method.
  176.         public void PrepareEditingControlForEdit(bool selectAll)
  177.         {
  178.             // No preparation needs to be done.
  179.         }
  180.  
  181.         // Implements the IDataGridViewEditingControl
  182.         // .RepositionEditingControlOnValueChange property.
  183.         public bool RepositionEditingControlOnValueChange
  184.         {
  185.             get
  186.             {
  187.                 return false;
  188.             }
  189.         }
  190.  
  191.         // Implements the IDataGridViewEditingControl
  192.         // .EditingControlDataGridView property.
  193.         public DataGridView EditingControlDataGridView
  194.         {
  195.             get
  196.             {
  197.                 return dataGridView;
  198.             }
  199.             set
  200.             {
  201.                 dataGridView = value;
  202.             }
  203.         }
  204.  
  205.         // Implements the IDataGridViewEditingControl
  206.         // .EditingControlValueChanged property.
  207.         public bool EditingControlValueChanged
  208.         {
  209.             get
  210.             {
  211.                 return valueChanged;
  212.             }
  213.             set
  214.             {
  215.                 valueChanged = value;
  216.             }
  217.         }
  218.  
  219.         // Implements the IDataGridViewEditingControl
  220.         // .EditingPanelCursor property.
  221.         public Cursor EditingPanelCursor
  222.         {
  223.             get
  224.             {
  225.                 return base.Cursor;
  226.             }
  227.         }
  228.  
  229.         protected override void OnValueChanged(EventArgs eventargs)
  230.         {
  231.             // Notify the DataGridView that the contents of the cell
  232.             // have changed.
  233.             valueChanged = true;
  234.             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
  235.             base.OnValueChanged(eventargs);
  236.         }
  237.     }
  238. }
  239.  
Code for the form containing the datagridview:
Expand|Select|Wrap|Line Numbers
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3.     DateTimePickerColumn col = new DateTimePickerColumn();
  4.     dataGridView1.Columns.Add(col);
  5.     dataGridView1.RowCount = 5;
  6. }
  7.  
Reference from MSDN 2005- How to: Host Controls in Windows Forms DataGridView Cells
Jul 24 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.