Dino Kantardzic
Dino Kantardzic

Reputation: 107

ListView showing date field as blank

Hello I have a listview with a few fields one of which is a datefield called DatumNabavke.

public DateTime? DatumNabavke
    {
        get { return _datumNabavke; }
        set { _datumNabavke = value; OnPropertyChanged("DatumNabavke"); }

    }

As you can see it is a nullable date field. The thing is that while it can be Null, when it is, it is saved automatically with DateTimeNow

if (adress.DatumNabavke == null)
                {
                    oleComd.Parameters[":datumnabavke"].Value = DateTime.Now;
                }

So when I create a new row and leave the DatumNabavke field empty, it should show as today's date in my ListView with GridView

<GridViewColumn   Header="Datum Nabavke" DisplayMemberBinding="{Binding DatumNabavke, StringFormat= d}" />

But for some reason this doesn't happen. Every other row that I have shown, name, id, Value, all are shown as is supposed to be but for some reason date is just blank.

Another thing, in my debug output I get the following error:

System.Windows.Data Error: 11 : Fallback value 'x:Static sys:DateTime.Now' (type 'String') cannot be converted for use in 'SelectedDate' (type 'Nullable1'). BindingExpression:Path=Adress.DatumZaduzenja; DataItem=null; target element is 'DatePicker' (Name='datumZaduzenja'); target property is 'SelectedDate' (type 'Nullable1') FormatException:'System.FormatException: x:Static sys:DateTime.Now is not a valid value for DateTime. ---> System.FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) --- End of inner exception stack trace --- at System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at System.ComponentModel.NullableConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at System.Windows.Data.BindingExpressionBase.ConvertValue(Object value, DependencyProperty dp, Exception& e)'

If any of you can help I would be much obliged.

Upvotes: 0

Views: 608

Answers (2)

Arthur Nunes
Arthur Nunes

Reputation: 7058

I assume DatumNabavke is a property in an ViewModel, and that a collection of that ViewModel have already been bound sucessfully to your ListView.ItemsSource. Have you tried:

public DateTime? DatumNabavke
{
    get { 
        if( _datumNabavke == null)
             return DateTime.Now;

        return _datumNabavke; 
    }
    set { _datumNabavke = value; OnPropertyChanged("DatumNabavke"); }

}

You didn't specify where you call the piece of code

if (adress.DatumNabavke == null) { oleComd.Parameters[":datumnabavke"].Value = DateTime.Now; }

If this happens after the ViewModel collection has been loaded as ItemSource, the binding will never work because this piece of code does not raise the property notification. You could also try this:

if (adress.DatumNabavke == null)
{
    oleComd.Parameters[":datumnabavke"].Value = DateTime.Now;
    OnPropertyChanged("DatumNabavke");
}

Upvotes: 1

James
James

Reputation: 82136

Have you tried specifying the format in a different attribute e.g.

<GridViewColumn Header="Datum Nabavke" DisplayMemberBinding="{Binding DatumNabavke}" StringFormat="{}{0:dd/MM/yyyy}" />

Upvotes: 1

Related Questions