date time picker binding bug

J

Johnaton

hi, I have this problem with the date time picker, when I bind the control
to data table and add new record, it is ok, however, if I delete all records
via a button using the
me.bindingcontext(datatable).removeat(me.bindingcontext(dataTable).position)
statement, it always throw the following error ..



A first chance exception of type 'System.ArgumentException' occurred in
system.dll



Additional information: '1/1/0001 12:00:00 AM' is not a valid value for
'Value'. 'Value' should be between 'MinDate' and 'MaxDate'.



microsoft KB
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q313513#kb5

did acknowledge this problem but it doesn't seem to solve this situation.



To simulate this problem, just create a table with a datetime field and bind
it to a window form date time picker control, then have 2 buttons, one to
add records via Me.BindingContext(dataTable).AddNew() ... and another to
delete the record via above mentioned code, when you are deleting the last
record, this error will popup even you include the format & parse codes as
recommend by MS. The problem here is that it doesn't even go to the
format/parse when before it throws this error..



Can anyone please help!



Thank in advance



Hanbert
 
C

Cor Ligthert

Johnaton,

It seems for me natural, when there is no date it gives this date so why
don't you check before the remove if the datatable.rows.count = 1 and than
remove the binding before.

That is what I would do and than when that does it, not look at if anymore

And test in an add of course if the rowtable = 1 and than add the binding
(it should than be empty).

Just my thought,

Cor
 
J

Johnaton

Hi Cor,



Thank for your reply, I have illustrated the problem with a simple example,
however, the application involved many date and other fields which made this
option not very viable. moreover, I'll need to add codes to put the binding
back again if user click the ADD button...and there is not single command
like the clear all bindings for the form, each controls must be unbind
individually and bind again individually...



What I can't understand is why the format & parse event handlers don't work
as suggested by Microsoft in this situation. where there are records, the
format & parse codes run. But upon deleting the last record, so how, it just
don't activate these codes.???



Can some experts out there enlighten me on this? Or point to any articles to
overcome this or even how to re-write (extent) this control .



Thank in advance .
 
C

Cor Ligthert

Johnaton,

There was something strange in your message. However I took it as you wrote.
Now I have made a little test for it, and in my opinion is there no
exception throwed.

\\\
Dim dt As New DataTable
Private Sub Form4_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("mydate", GetType(System.DateTime))
For i As Integer = 0 To 10
dt.Rows.Add(dt.NewRow)
dt.Rows(i)(0) = Now.AddDays(-i)
Next
Me.DateTimePicker1.DataBindings.Add("Value", dt, "mydate")
BindingContext(dt).Position = 10
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If dt.Rows.Count > 0 Then
dt.Rows.RemoveAt(BindingContext(dt).Position)
Else
MessageBox.Show("there are no more rows to remove")
End If
End Sub
///

So what do we do wrong?

Cor
 
J

Johnaton

Hi Cor,

Thank again for the reply .. don't understand what you mean by strange ...
I pasted your codes .... the exception error did throw ... don't it happen
on yours???

when I click the button1 on the 10th time ... ie. when count = 1 and after
it deleted the very last record, that exception message was thrown .... I
would be very interested to know if it doesn't happen on yours ..

that is what I mean, unless I write
If dt.Rows.Count = 1 Then
...whole chunk of codes to do unbinding ...
else
dt.Rows.RemoveAt(BindingContext(dt).Position)
end if
..... and some where must check again to bind all controls again ..

Thank & will be interested to hear from you on the above ..
 
C

Cor Ligthert

Johnaton,

When it was thrown I would have told you, the messagebox is showed.

Mayby it is in the SP1 of the framework.

Cor
 

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