datetime

  • Thread starter Thread starter John
  • Start date Start date
J

John

I have a date value in a database and I want to show it's value on a form.
It is bound to the data and shown with a datetimepicker. When the value in
the database is null, an incorect value is shown on the form. is there a
way to get null to show when it is null?
 
I have a date value in a database and I want to show it's value on a form.
It is bound to the data and shown with a datetimepicker. When the value in
the database is null, an incorect value is shown on the form. is there a
way to get null to show when it is null?

The code below will work, but there must be another way ;o)

public partial class testForm : Form
{
// Form with DateTimePicker and two Buttons
// This is a kludge but it works.
// There must be a better way????

private DateTime? now;

public testForm()
{
InitializeComponent();
}

private void NotNullButton_Click(object sender, EventArgs e)
{
now = DateTime.Now;
dateTimePicker1.CustomFormat = "MM/dd/yyyy";
dateTimePicker1.Value = (DateTime)now;
}

private void testForm_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
}

private void NullButton_Click(object sender, EventArgs e)
{
now = null;
if(now == null)
dateTimePicker1.CustomFormat = "null";
}
}
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Hi John,

Yes, when we bind a datetime value in a database to a datetimepicker, if
the value in the database is null, the datetimepicker will show the current
datetime in it, which is not the correct value.

In fact, when we bind a data source to a property of a control, we add a
Binding object into the DataBindings property of the control. There's an
event called Format in the binding object, which occurs when the property
of a control is bound to a data value. So we could subscribe and handle
this event to set the CurrentFormat property of the datetimepicker to
'null' when the value in the data source is null.

The following is a sample.

private void Form1_Load(object sender, EventArgs e)
{
// set the Format property to DateTimePickerFormat.Custom, so
that we could set the CustomFormat to 'null' when necessary
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.DataBindings[0].Format += new
ConvertEventHandler(Form1_Format);

// add the code to fill in the data source
.........

}

void Form1_Format(object sender, ConvertEventArgs e)
{
// If the value in the data source is null, set the
CustomFormat property of the datetimepicker to 'null'
if (e.Value is DBNull)
{
this.dateTimePicker1.CustomFormat = "null";
}
// If the value in the data source is not null, set the
CustomFormat property to the normal format
else
{
this.dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi John,

Yes, when we bind a datetime value in a database to a datetimepicker, if
the value in the database is null, the datetimepicker will show the current
datetime in it, which is not the correct value.

In fact, when we bind a data source to a property of a control, we add a
Binding object into the DataBindings property of the control. There's an
event called Format in the binding object, which occurs when the property
of a control is bound to a data value. So we could subscribe and handle
this event to set the CurrentFormat property of the datetimepicker to
'null' when the value in the data source is null.

The following is a sample.

private void Form1_Load(object sender, EventArgs e)
{
// set the Format property to DateTimePickerFormat.Custom, so
that we could set the CustomFormat to 'null' when necessary
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.DataBindings[0].Format += new
ConvertEventHandler(Form1_Format);

// add the code to fill in the data source
.........

}

void Form1_Format(object sender, ConvertEventArgs e)
{
// If the value in the data source is null, set the
CustomFormat property of the datetimepicker to 'null'
if (e.Value is DBNull)
{
this.dateTimePicker1.CustomFormat = "null";
}
// If the value in the data source is not null, set the
CustomFormat property to the normal format
else
{
this.dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Much more elegant than mine Linda :o)
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Back
Top