Groups | Blog | Home
all groups > dotnet ado.net > december 2006 >

dotnet ado.net : Binding DateTimePicker


John Stivenson
12/27/2006 2:06:01 AM
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?
Cor Ligthert [MVP]
12/27/2006 12:11:34 PM
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

"John Stivenson" <JohnStivenson@discussions.microsoft.com> schreef in
bericht news:A8617AA3-F6BD-4E37-A34C-5C73633CFB04@microsoft.com...
[quoted text, click to view]

Bart Mermuys
12/28/2006 12:06:28 PM
Hi,

[quoted text, click to view]

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.

[quoted text, click to view]

Try to set default values on the DataSource.

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

HTH,
Greetings




[quoted text, click to view]

John Stivenson
12/30/2006 1:42:00 PM
[quoted text, click to view]

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

Bart Mermuys
12/31/2006 8:05:26 AM
Hi,

[quoted text, click to view]

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


[quoted text, click to view]

AddThis Social Bookmark Button