Binding DateTimePicker

G

Guest

A DateTimePicker control on my form is bound to a datetime field in a
DataTable.
When inserting a new record the default value for DateTimePicker is today's
date.
However, unless the user changes this date it is not saved to database (NULL
is stored instead of today's date). Text fields are stored normally.

A TextBox control has similar behavior: if the user leaves
a text box blank, NULL is sent to database, but if he enters
some text and then deletes it, an empty string is stored.
It seems that there is some "Modified" property for every control.

In this case I want to store the current date instead of NULL.
How can I achive this?
 
C

Cor Ligthert [MVP]

John,

What have you binded because as this question comes the reason is mostly
that there is binded to the text property from the datetime picker. That has
to the value property, normally this should go fluently.

Cor
 
B

Bart Mermuys

Hi,

John Stivenson said:
A DateTimePicker control on my form is bound to a datetime field in a
DataTable.
When inserting a new record the default value for DateTimePicker is
today's
date.
However, unless the user changes this date it is not saved to database
(NULL
is stored instead of today's date). Text fields are stored normally.

A TextBox control has similar behavior: if the user leaves
a text box blank, NULL is sent to database, but if he enters
some text and then deletes it, an empty string is stored.
It seems that there is some "Modified" property for every control.

That right, well it's the Binding that has a (private) modified flag which
is triggered by the Control property-Changed event (eg.
TextChanged/ValueChanged). The value isn't persisted if the modified flag
hasn't been set.
In this case I want to store the current date instead of NULL.
How can I achive this?

Try to set default values on the DataSource.

If you're using NET2.0 you can use the DataTable.TableNewRow event.

HTH,
Greetings
 
G

Guest

Bart Mermuys said:
Try to set default values on the DataSource.

If you're using NET2.0 you can use the DataTable.TableNewRow event.

It seems as a good solution, but I don't know how to do this.
Where should I add TableNewRow event handler?

Thanks in advance.
 
B

Bart Mermuys

Hi,

John Stivenson said:
It seems as a good solution, but I don't know how to do this.
Where should I add TableNewRow event handler?

It depends on a couple of things...

First, you don't mention that you are using NET2.0, the event is only
available in NET2.0, since you consider the option i assume you are.

Second, it would also be different depending on how and where you create the
DataSet/DataTable which you have bound.

And last, the language you are using.


// Assuming you are using NET2.0, you've added the
// (typed) DataSet/DataTable using the designer and are
// using C#, then you would do something like:
public partial class SomeForm : Form
{
public SomeForm()
{
InitializeComponent();

// add handler after InitializeComponent
someDataSet.someDataTable.TableNewRow +=
new DataTableNewRowEventHandler( OnSomeTable_TableNewRow );
}

private void OnSomeTable_TableNewRow( object sender,
DataTableNewRowEventArgs e )
{
// set default values
e.Row["someField1"] = ....;
e.Row["someField2"] = ...;
...
}
}

HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top