المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : ObservableCollection bound to ListView



C# Programming
09-19-2009, 11:52 PM
Problem
WPF Project (VS 2008)

I'm using a ListView (with GridView) bound to an ObservableCollection. The collection includes a DateTime field.
When I populate the collection, the DateNR field is often null, but then the ListView displays a date of "01-Jan-01"!!

How do I stop this as I don't want the user to see a date when there shouldn't be one. I need the DateNR field in the collection to be a DateTime value not a string because this column is used for sorting.

Any ideas would be welcome. thanks


public class RecordData
{
public int ID { get; set; }
public DateTime DateNR { get; set; }
}

Adding GridView Column
GridViewColumnHeader gvch = new GridViewColumnHeader();
GridViewColumn gvc = new GridViewColumn();
Binding bind = new Binding("DateNR");
bind.Converter = new FormattingConverter();
bind.ConverterParameter = @"{0:dd-MMM-yy}";
gvc.Width = 80;
gvc.DisplayMemberBinding = bind;
gvch.Content = "NR Date";
gvc.Header = gvch;
myGridView.Columns.Add(gvc);

Populate Collection
SqlConnection sqlConn = new SqlConnection(myConnectionString);
SqlCommand sqlComm = new SqlCommand("...", sqlConn);
SqlDataReader sqlDR = sqlComm.ExecuteReader();
// ...
RecordData rd = new RecordData();
rd.ID = sqlDR.GetInt32(0);
if (sqlDR.IsDBNull(1) == false)
rd.DateNR = sqlDR.GetDateTime(1);
// ...
_MyCollection.Add(rd);