Alaa Jabre
Alaa Jabre

Reputation: 1883

Set default value of DatePicker in DataGrid column

I Have a DataGrid bound to a Collection, one of the columns type is DateTime.

my xaml code is:

<DataGridTemplateColumn Width="260" Header="OrderDate">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <DatePicker SelectedDate="{Binding OrderDate,
                                       Mode=TwoWay,
                                       UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

the problem is the DatePicker default value is 1/1/0001.

how do I set the default value?

Upvotes: 2

Views: 1103

Answers (2)

Alaa Jabre
Alaa Jabre

Reputation: 1883

Problem was fixed this way(sorry if the question wasn't clear):

in the class declaration:

public DateTime OrderDate { get; set; }

change it to:

public DateTime? OrderDate { get; set; }

it look like it was taking the DateTime dfault value and the solution was to add nulls instead.

Upvotes: 2

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

You can add a property like this:

public class SomeClass
{
    // Default value
    private DateTime myDateTime = new DateTime(2010, 1, 18);

    public string Sample
    {
        get;
        set;
    }

    public DateTime OrderDate
    {
        get
        {
            return myDateTime;
        }

        set
        {
            myDateTime = value;
        }
    }
}

After adding the data, for example - in ObservableCollection:

SomeCollection.Add(new SomeClass()
{            
    Sample = "Sample1",
    OrderDate = DateTime.Now, // Forcibly set date
});     

When skipping of this parameter is added to the default value:

SomeCollection.Add(new Person()
{            
    Sample = "Sample1",
    // Skipping date, set the default value "2010, 1, 18"
});     

Upvotes: 0

Related Questions