DateTimePicker problem with .AddNew

M

Mika M

Hi!

My Windows Form contains DateTimePicker to show date-type data from
DataTable. Because application need to be able to save "no date" ie.
NULL into database table, I solved it like way...

("dtpRepaired" is DateTimePicker-control)

dtpRepaired.Checked = Not IsDBNull(dt.Rows(cm.Position)("Repaired"))

If dtpRepaired.Checked Then
dtpRepaired.Value = Date.Parse(dt.Rows(cm.Position)("Repaired").ToString())
End If

Private Sub dtpRepaired_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dtpRepaired.TextChanged
dt.Rows(cm.Position)("Repaired") = IIf(dtpRepaired.Checked,
dtpRepaired.Value, DBNull.Value)
End Sub

....because I didn't succeeded to do this like this way bounding it
directly to the DataTable...

'dtpRepaired.DataBindings.Add(New Binding("Text", dt, "Repaired"))


This is almost working fine, but it occurs an error after cm.AddNew when
trying to change dtpRepaired-controls value or Checked-state.

Error message is: "There is no row at Position 5.". I understand this
why, but don't know how I could solve to make it possible to "add new"
and make it possible to edit dtpRepaired too without this problem. Any
succestions?

Is it really impossible to bound DateTimePicker-control to DataTable,
and make it possible to change date in DataTable to "NULL" ?


Hopefully you understand my problem with my poor english, I'm finnish :)
 
C

Cor Ligthert

Mika,

Did you try what the databinding events can do for you. Here a sample I made
with a textbox, can not be that different in my opinion with the
datetimepicker.

\\\
Private Sub myroutine()
Mybinding = New Binding("Text", ds.Tables(0), "mydatfield")
textdatfield.DataBindings.Add(Mybinding)
AddHandler mybinding.Format, AddressOf DBdateTextbox
AddHandler mybinding.Parse, AddressOf TextBoxDBdate
End sub
Private Sub DBdateTextbox(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
datum = CDate(cevent.Value)
cevent.Value = datum.ToString("d")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
End If
End Sub
///

I hope this helps a little bit?

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