?
Solved

datatimepicker .net 2.0

Posted on 2006-05-16
5
Medium Priority
?
439 Views
Last Modified: 2008-01-09
fo the datetimepicker, I have some values that are originally null.  Is there a way of keeping the value of the datetimepicker as null or does it always have to have a value?
0
Comment
Question by:yanci1179
5 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 400 total points
ID: 16695300
I wrote an article in January 2005 (available from http://emoreau.s2i.com/) for .Net 1.1. Maybe it will help!
0
 
LVL 11

Accepted Solution

by:
anyoneis earned 800 total points
ID: 16695311
It always has to have a value.

The best you can do, without deriving your own control, is to come up with a value that the users "understand" means no date given and to use the format and parse events to translate from/to DBNull. And example program follows:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace BindDateTimePicker
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            DataSet _ds = new DataSet("MyDataSet");
            DataTable _tbl = new DataTable("MyTable");
            String _nullDate = DateTime.Parse("01/01/1800").ToLongDateString();
           
 
            private System.Windows.Forms.Button previousButton;
            private System.Windows.Forms.Button nextButton;
            private System.Windows.Forms.Label IDLabel;
            private System.Windows.Forms.TextBox IDTextBox;
            private System.Windows.Forms.Label dateLabel;
            private System.Windows.Forms.DateTimePicker dateDateTimePicker;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
 
            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();
 
                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }
 
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }
 
            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.previousButton = new System.Windows.Forms.Button();
                  this.nextButton = new System.Windows.Forms.Button();
                  this.IDLabel = new System.Windows.Forms.Label();
                  this.IDTextBox = new System.Windows.Forms.TextBox();
                  this.dateLabel = new System.Windows.Forms.Label();
                  this.dateDateTimePicker = new System.Windows.Forms.DateTimePicker();
                  this.SuspendLayout();
                  //
                  // previousButton
                  //
                  this.previousButton.Location = new System.Drawing.Point(32, 200);
                  this.previousButton.Name = "previousButton";
                  this.previousButton.TabIndex = 0;
                  this.previousButton.Text = "Previous";
                  this.previousButton.Click += new System.EventHandler(this.previousButton_Click);
                  //
                  // nextButton
                  //
                  this.nextButton.Location = new System.Drawing.Point(192, 200);
                  this.nextButton.Name = "nextButton";
                  this.nextButton.TabIndex = 1;
                  this.nextButton.Text = "Next";
                  this.nextButton.Click += new System.EventHandler(this.nextButton_Click);
                  //
                  // IDLabel
                  //
                  this.IDLabel.Location = new System.Drawing.Point(8, 16);
                  this.IDLabel.Name = "IDLabel";
                  this.IDLabel.Size = new System.Drawing.Size(48, 23);
                  this.IDLabel.TabIndex = 2;
                  this.IDLabel.Text = "ID:";
                  //
                  // IDTextBox
                  //
                  this.IDTextBox.Location = new System.Drawing.Point(56, 16);
                  this.IDTextBox.Name = "IDTextBox";
                  this.IDTextBox.TabIndex = 3;
                  this.IDTextBox.Text = "textBox1";
                  //
                  // dateLabel
                  //
                  this.dateLabel.Location = new System.Drawing.Point(8, 80);
                  this.dateLabel.Name = "dateLabel";
                  this.dateLabel.Size = new System.Drawing.Size(48, 23);
                  this.dateLabel.TabIndex = 4;
                  this.dateLabel.Text = "Date:";
                  //
                  // dateDateTimePicker
                  //
                  this.dateDateTimePicker.Location = new System.Drawing.Point(56, 80);
                  this.dateDateTimePicker.Name = "dateDateTimePicker";
                  this.dateDateTimePicker.Size = new System.Drawing.Size(272, 22);
                  this.dateDateTimePicker.TabIndex = 5;
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
                  this.ClientSize = new System.Drawing.Size(384, 260);
                  this.Controls.Add(this.dateDateTimePicker);
                  this.Controls.Add(this.dateLabel);
                  this.Controls.Add(this.IDTextBox);
                  this.Controls.Add(this.IDLabel);
                  this.Controls.Add(this.nextButton);
                  this.Controls.Add(this.previousButton);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.ResumeLayout(false);
 
            }
            #endregion
 
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }
 
            private void Form1_Load(object sender, System.EventArgs e)
            {
                  // Set up a fake dataset for testing
                  _ds = new DataSet("MyDataSet");
                  _tbl = new DataTable("MyTable");
                  _ds.Tables.Add(_tbl);
 
                  _tbl.Columns.Add("ID", typeof(System.Int32));
                  _tbl.Columns[0].AutoIncrement = true;
                  _tbl.Columns[0].AutoIncrementSeed = 1;
                  _tbl.Columns[0].AutoIncrementStep = 1;
 
                  _tbl.Columns.Add("Date", typeof(System.DateTime));
                  _tbl.Columns[1].AllowDBNull = true;
 
                  DataRow dr = _tbl.NewRow();
                  dr["Date"] = new DateTime(2006, 01, 01);
                  dr.EndEdit();
                  _tbl.Rows.Add(dr);;
 
                  dr = _tbl.NewRow();
                  dr.EndEdit();
                  _tbl.Rows.Add(dr);
 
                  // Now do the binding
                  Binding db = new Binding("Text", _tbl, "ID");
                  IDTextBox.DataBindings.Add(db);
                 
                  db = new Binding("Text", _tbl, "Date");
                  db.Format += new ConvertEventHandler(db_Format);
                  db.Parse +=new ConvertEventHandler(db_Parse);
                  dateDateTimePicker.DataBindings.Add(db);
            }
 
            private void db_Format(object sender, ConvertEventArgs e)
            {
                  if (e.Value == System.DBNull.Value)
                  {
                        e.Value = _nullDate;
                  }
            }
 
            private void db_Parse(object sender, ConvertEventArgs e)
            {
                  if (e.Value.ToString() == _nullDate)
                  {
                        e.Value = System.DBNull.Value;
                  }
            }
 
            private void previousButton_Click(object sender, System.EventArgs e)
            {
                  this.BindingContext[_tbl].Position -= 1;
            }
 
            private void nextButton_Click(object sender, System.EventArgs e)
            {
                  this.BindingContext[_tbl].Position += 1;
            }
      }
}
0
 
LVL 14

Assisted Solution

by:Shiju Sasidharan
Shiju Sasidharan earned 400 total points
ID: 16696338
alternatively ,
use a check box to indicate that datepicker has value
that is if check box is checked then enable datepicker
otherwise disable or hide datepicker
0
 
LVL 19

Assisted Solution

by:Fahad Mukhtar
Fahad Mukhtar earned 400 total points
ID: 16696881
0
 

Author Comment

by:yanci1179
ID: 16702203
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question