473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Problem in datagridview datetimepicker column.

88 64KB
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
0 5624

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

Similar topics

3
by: Peter | last post by:
I need to add a button to a DataGridView, but the button should only show up in the cell when user clicks on a cell in the DataGridView and disapear when user clicks off the cell. Does anyone know...
0
by: Bob | last post by:
1- Inability to easily reset datagridview combobox selection to null Solution - add a contextmenustrip to the column in which you have the combobox on the datagridview. show to user, something like...
3
by: Alex Bibiano | last post by:
żAre there free column types to use in my DataGridView? For exemple a datapicker, a maskedit, or a numeric type I know I can do it, but I'm searching for some implemented code.
1
by: Rich | last post by:
Hello, I am reading data from a sql server table that is under replication. This table has the replicatin GUID column that is generated with replicatin. I am reading the data from a...
2
by: Bob | last post by:
How do I show a calendar control or another appropriate control to pick a date and time for a cell in a column in a datagridview? Thanks for any help. Bob
1
by: Bob | last post by:
Hi, I want to have a datetimepicker in a datecolumn. Found the source for creating a DTP column, but can't see how to convert an existing date column to be a DTP column. ie. My DataGridView is...
2
by: =?Utf-8?B?QXJuZSBCZXJ1bGRzZW4=?= | last post by:
I've downloaded some code relative to establishing a column on my DataGridView which allows me to use the DateTimePicker. So far so good. If I add a column using dim col as new CalendarColumn...
0
by: eddyman592 | last post by:
Ok, I'm trying to make a column in my data grid be a DateTimePicker column. I copied and pasted all the code from the msdn website. Now, the difference in my code and the microsoft code is that my...
0
by: priyamtheone | last post by:
Can anyone provide me with a code example of how to use a datetimepicker cell in datagridview where in the datetimepicker column i can set the value of any cell to null or to any datetime according...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.