473,761 Members | 8,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nearly Finished Null Enabled DateTimePicker

5 New Member
Hi Everyone,

There are so many Nullable DateTimePickers out there that this post may not even be found among all the others, but I wanted to try. First, I want to thank all the authors of the code I used as a starting point. Second, I hope this code helps someone as much as they helped me.

I have built a Nullable DateTimePicker and NullableCalenda rColumn/Cell/EditingControl that is nearly 100% complete, as far as I can tell. There is only one remaining issue and it has me stalled.

When the user clicks into the cell containing the Nullable control (pleaced in a DataGridView), the first time, and only the first time, instead of the date already there, today's date appears. As an example, say that the cell contained 1/1/08, the DataGridView has just been created, and the user clicks (twice) in the cell to enable the EditingControl. Instead of 1/1/08, the date that appears is 7/20/08. Again, this happens only the very first time the control is used. After that it works as it should, initializing the control to the date in the cell.

Any thoughts?

Todd

P.S. Anyone that wants to expand on this or make disparaging comments about my code, feel free ... I can use all the help I can get!

*************** *************** *************** *************** **
NullableDateTim ePicker.cs
*************** *************** *************** *************** **
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7.  
  8. namespace Custom
  9. {
  10.     public class NullableDateTimePicker : System.Windows.Forms.DateTimePicker
  11.     {
  12.         private Boolean isNull = false;
  13.         private DateTimePickerFormat baseFormat = DateTimePickerFormat.Short;
  14.         private Boolean ignoreBindOnFormat = false;
  15.  
  16.         public NullableDateTimePicker()
  17.         {
  18.             this.Format = baseFormat;
  19.             if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
  20.         }
  21.  
  22.         public Boolean IsNull
  23.         {
  24.             get { return isNull; }
  25.             set
  26.             {
  27.                 isNull = value;
  28.                 this.Checked = value;
  29.             }
  30.         }
  31.  
  32.         //TODO: Add BaseCustomFormat
  33.  
  34.         public DateTimePickerFormat BaseFormat
  35.         {
  36.             get { return baseFormat; }
  37.             set { baseFormat = value; }
  38.         }
  39.  
  40.         public object BindMe
  41.         {
  42.             get
  43.             {
  44.                 if (IsNull) return System.DBNull.Value;
  45.                 else return base.Value;
  46.             }
  47.             set
  48.             {
  49.                 String s = this.Name;
  50.  
  51.                 if (ignoreBindOnFormat) return;
  52.  
  53.                 if (System.Convert.IsDBNull(value))
  54.                 {
  55.                     // for some reason setting base.format in this.format calls set BindMe.
  56.                     // we need to ignore the following call
  57.                     ignoreBindOnFormat = true;
  58.                     this.Format = DateTimePickerFormat.Custom;
  59.                     this.CustomFormat = " ";
  60.                     IsNull = true;
  61.                     ignoreBindOnFormat = false;
  62.                 }
  63.                 else
  64.                 {
  65.                     this.Format = baseFormat;
  66.                     if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
  67.                     IsNull = false;
  68.                     base.Value = (DateTime)value;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         protected override void OnKeyDown(KeyEventArgs e)
  74.         {
  75.             base.OnKeyDown(e);
  76.  
  77.             if (e.KeyCode == Keys.Delete)
  78.             {
  79.                 this.BindMe = DBNull.Value;
  80.             }
  81.         }
  82.  
  83.         protected override void OnCloseUp(EventArgs eventargs)
  84.         {
  85.             base.OnCloseUp(eventargs);
  86.             BindMe = base.Value;
  87.         }
  88.     }
  89. }
  90.  
  91.  
*************** *************** *************** *************** **
NullableDateTim ePicker.cs
*************** *************** *************** *************** **
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. using System.Drawing;
  8.  
  9. namespace Custom
  10. {
  11.  
  12.     public class NullableCalendarColumn : DataGridViewColumn
  13.     {
  14.  
  15.         public NullableCalendarColumn() : base(new NullableCalendarCell()) { }
  16.  
  17.         public NullableCalendarColumn(string frm) : base(new NullableCalendarCell(frm)) { }
  18.  
  19.         public void setValue(string frm)
  20.         {
  21.             base.CellTemplate.Value = frm;
  22.         }
  23.  
  24.         public override DataGridViewCell CellTemplate
  25.         {
  26.             get { return base.CellTemplate; }
  27.             set
  28.             {
  29.                 // Ensure that the cell used for the template is a CalendarCell.
  30.                 if (value != null && !value.GetType().IsAssignableFrom(typeof(NullableCalendarCell)))
  31.                 {
  32.                     throw new InvalidCastException("Must be a NullableCalendarCell");
  33.                 }
  34.  
  35.                 base.CellTemplate = value;
  36.             }
  37.         }
  38.     }
  39.  
  40.     public class NullableCalendarCell : DataGridViewTextBoxCell
  41.     {
  42.         private String baseFormat = "MM/dd/yyyy";
  43.  
  44.         public NullableCalendarCell()
  45.             : base()
  46.         {
  47.             // Use the short date format.
  48.             // Change the format here for the normal display.
  49.             this.Style.Format = baseFormat;
  50.         }
  51.  
  52.         public NullableCalendarCell(String frm)
  53.             : base()
  54.         {
  55.             baseFormat = frm;
  56.             this.Style.Format = frm;
  57.         }
  58.  
  59.         public String BaseFormat
  60.         {
  61.             get { return baseFormat; }
  62.             set { baseFormat = value; }
  63.         }
  64.  
  65.         public override void InitializeEditingControl(int rowIndex,
  66.             object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
  67.         {
  68.             // Set the value of the editing control to the current cell value.
  69.             base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
  70.  
  71.             NullableCalendarEditingControl ctl = DataGridView.EditingControl as NullableCalendarEditingControl;
  72.  
  73.             if (this.Value.ToString() == "")
  74.             {
  75.                 ctl.BindMe = DBNull.Value;
  76.             }
  77.             else
  78.             {
  79.                 ctl.Value = (DateTime)this.Value;
  80.                 ctl.Text = this.Value.ToString();
  81.                 ctl.EditingControlFormattedValue = this.Value.ToString();
  82.                 ctl.IsNull = false;
  83.                 ctl.BindMe = (DateTime)this.Value;
  84.                 ctl.Left = 100;
  85.                 ctl.Name = "toddsctl";
  86.             }
  87.         }
  88.  
  89.  
  90.         public override Type EditType
  91.         {
  92.             get
  93.             {
  94.                 // Return the type of the editing contol that CalendarCell uses.
  95.                 return typeof(NullableCalendarEditingControl);
  96.             }
  97.         }
  98.  
  99.         public override Type ValueType
  100.         {
  101.             get
  102.             {
  103.                 // Return the type of the value that CalendarCell contains.
  104.                 return typeof(String);
  105.             }
  106.         }
  107.  
  108.         public override object DefaultNewRowValue
  109.         {
  110.             get
  111.             {
  112.                 // Use the current date and time as the default value.
  113.                 return DateTime.Now.ToString();
  114.             }
  115.         }
  116.  
  117.         public override void DetachEditingControl()
  118.         {
  119.             base.DetachEditingControl();
  120.  
  121.             if (this.EditedFormattedValue.ToString().Length == 0)
  122.             {
  123.                 this.Value = DBNull.Value;
  124.             }
  125.         }
  126.  
  127.         protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
  128.         {
  129.             base.OnKeyDown(e, rowIndex);
  130.  
  131.             if (e.KeyCode == Keys.Delete) this.Value = DBNull.Value;
  132.         }
  133.     }
  134.  
  135.     class NullableCalendarEditingControl : NullableDateTimePicker, IDataGridViewEditingControl
  136.     {
  137.         DataGridView dataGridView;
  138.         private bool valueChanged = false;
  139.         int rowIndex;
  140.  
  141.         public NullableCalendarEditingControl()
  142.         {
  143.             // change the customformat here for how it shows when editing
  144.             this.Format = this.BaseFormat;
  145.         }
  146.  
  147.         // Implements the IDataGridViewEditingControl.EditingControlFormattedValue
  148.         // property.
  149.         public object EditingControlFormattedValue
  150.         {
  151.             get { return this.BindMe.ToString(); }
  152.             set
  153.             {
  154.                 if (value == null || value as String == "") this.BindMe = DBNull.Value;
  155.                 else
  156.                 {
  157.                     this.BindMe = DateTime.Parse((String)value);
  158.                     //this.Value = DateTime.Parse((String)value);
  159.                 }
  160.             }
  161.         }
  162.  
  163.         // Implements the
  164.         // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
  165.         public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
  166.         {
  167.             return EditingControlFormattedValue;
  168.         }
  169.  
  170.         // Implements the
  171.         // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
  172.         public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
  173.         {
  174.             this.Font = dataGridViewCellStyle.Font;
  175.             this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
  176.             this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
  177.         }
  178.  
  179.         // Implements the IDataGridViewEditingControl.EditingControlRowIndex property.
  180.         public int EditingControlRowIndex
  181.         {
  182.             get { return rowIndex; }
  183.             set { rowIndex = value; }
  184.         }
  185.  
  186.         // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey method.
  187.         public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
  188.         {
  189.             // Let the DateTimePicker handle the keys listed.
  190.             switch (key & Keys.KeyCode)
  191.             {
  192.                 case Keys.Left:
  193.                 case Keys.Up:
  194.                 case Keys.Down:
  195.                 case Keys.Right:
  196.                 case Keys.Home:
  197.                 case Keys.End:
  198.                 case Keys.PageDown:
  199.                 case Keys.PageUp:
  200.                 case Keys.Tab:
  201.                 case Keys.Delete:
  202.                     return true;
  203.                 default:
  204.                     return false;
  205.             }
  206.         }
  207.  
  208.         // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit method.
  209.         public void PrepareEditingControlForEdit(bool selectAll)
  210.         {
  211.             // No preparation needs to be done.
  212.         }
  213.  
  214.         // Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange property.
  215.         public bool RepositionEditingControlOnValueChange
  216.         {
  217.             get { return false; }
  218.         }
  219.  
  220.         // Implements the IDataGridViewEditingControl.EditingControlDataGridView property.
  221.         public DataGridView EditingControlDataGridView
  222.         {
  223.             get { return dataGridView; }
  224.             set { dataGridView = value; }
  225.         }
  226.  
  227.         // Implements the IDataGridViewEditingControl.EditingControlValueChanged property.
  228.         public bool EditingControlValueChanged
  229.         {
  230.             get { return valueChanged; }
  231.             set { valueChanged = value; }
  232.  
  233.         }
  234.  
  235.         // Implements the IDataGridViewEditingControl.EditingPanelCursor property.
  236.         public Cursor EditingPanelCursor
  237.         {
  238.             get { return base.Cursor; }
  239.         }
  240.  
  241.         protected override void OnValueChanged(EventArgs eventargs)
  242.         {
  243.             // Notify the DataGridView that the contents of the cell have changed.
  244.             valueChanged = true;
  245.             this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
  246.             base.OnValueChanged(eventargs);
  247.         }
  248.     }
  249. }
Jul 21 '08 #1
0 2601

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

Similar topics

1
2434
by: Dean L. Howen | last post by:
Dear friends, Could you help me to solve the problem: How can we set/get NULL values of datetime in DatetimePicker ? I design a form using DateTimePicker to input/output date value, but the date could be NULL Thank you.
7
3857
by: JJ | last post by:
Hi, What's the correct code for checking if a DateTime control is empty or not filled in with a date? Thanks, JJ
0
1275
by: Bernie Yaeger | last post by:
How can I use a datetimepicker as the control for entry of, say, enddate, when enddate may be any date but also may be null (because the end hasn't been reached yet)? Also, can I set up a datetimepicker that will allow manual entry instead of a pick from the calendar, such that the user can simply enter '09/23/2004'? Thanks for any help. Bernie Yaeger
2
17210
by: AA | last post by:
Do anyone know how to set null value to DateTimePicker control? I want to not displaying any date on the DateTimePicker, is it possible? Thx in advance! Richard
0
1169
by: Benjamin Bittner | last post by:
hallo ng, first of all, for the zipping progress i use the ziplib from http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx now to my problem. im trying to build a little application for zipping our webservers logfiles. after zipping all files, i've tried to delete all files, but i couldnt. i got an error message that i cannot delete a file, because it is still in use. Here my two functions for that:
7
2585
by: Pieter | last post by:
Hi, I'm looking for a DateTimePicker that supports 100% databinding (and support null-values)... It's a shame that the normal control doesn't, but when searching on the internet, I didn't find any solution that works without problems. Does anybody know where I can find an inherited DateTimePicker that supports databinding and nulll-values? Any links, sample code etc would be really appreciated! It can be for VB.NET 2005, in case that...
5
2710
by: M Skabialka | last post by:
I am creating my first Visual Studio project, an inventory database. I have created a form and used written directions to add data from a table to the form using table adapters, data sets, etc. I imported a table from an Access database which has some date fields. Many of these dates have never been filled in, so are nulls. If I look directly into the table data from Visual Studio it shows NULL. However, when I scroll from record to...
2
8195
by: Claes Wedin | last post by:
My customer needs a DateTimePicker in VS2005 C# that can show emty date values (blank/space/null). I need a control that: 1. Can show a blank value 2. Detete a date when hitting the delete and/or back key 3. Allowse the user to enter yyyy-MM-dd date format manually 4. When tabbed into the field behaves juast like a textbox
4
3300
by: jehugaleahsa | last post by:
Hello: We were hoping to allow users to have DateTimePicker value null so that a unused date is stored on a Database. Does the control support this? I am pretty sure it doesn't. How would you go about representing this to a user so they are not confused? Thanks,
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.