Databinding DateTime Picker value to Object proberty with Bindingsource

Hobbyist_programmer 621 Reputation points
2022-03-25T21:30:31.07+00:00

Hallo,

I have a custom list of objects and the object has a property called "QuD"

Public Property QuD As Date = Date.Now

and I am binding this to a DateTime picker like below

DateTimePicker1.Databindings.Add("Value",BindingSource1,"QuD", True,  DataSourceUpdateMode.OnPropertyChanged)

When i initialise this object i am getting following exception

System.ArgumentOutOfRangeException: 'Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'.
Parameter name: Value'

Any idea how to solve this issue?.

I can bind it to "text" instead of Value but it creates problems in other language systems ..like German pc settings.

Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,741 questions
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2022-03-25T23:55:50.187+00:00

    Hi
    Not exactly sure if this is of any help, but here it is - just ignore if not.

    187134-freddy.gif

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
    		Dim tests(3) As test  
    		tests(0) = New test With {.int = 1, .dat = Now.Date, .str = "Freddy"}  
    		tests(1) = New test With {.int = 2, .dat = Now.AddDays(-11), .str = "Freddy2"}  
    		tests(2) = New test With {.int = 3, .dat = Now.AddDays(11), .str = "Freddy3"}  
    		tests(3) = New test With {.int = 4, .dat = Now.AddDays(-111), .str = "Freddy4"}  
    		DataGridView1.DataSource = tests  
      
      
    		DateTimePicker1.DataBindings.Add("value", tests, "dat", True, DataSourceUpdateMode.OnPropertyChanged)  
      
    	End Sub  
    	Class test  
    		Property int As Integer  
    		Property dat As Date  
    		Property str As String  
    	End Class  
    End Class  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2022-03-28T08:37:49.277+00:00

    Hallo LesHay,

    Thanks for the answer, It seems to be the answer but unfortunately it does not work with my example. I dont know how to explain it . I tried to make an example but it does not give me this erorr.

    I am initializing a custom list of objects which is empty when initialized, but this object has a Date property that throws this exception.

    e.g

    Public Class Test
    Public Property Toys as New Toys
    End Class    
    
    
    Public Class Toy
    Public Property QuD As Date = Date.Today
    End Class
    
    
    Public Class Toys
        Inherits System.ComponentModel.BindingList(Of Toy)
    End Class
    
    
    When I initialize or add a new Test object to the list ..it gives that exception
     
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.