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

مشاهدة النسخة كاملة : ListView Binding - ObservableCollection



C# Programming
05-02-2009, 05:15 PM
Overview
I am using a ListView (with GridView) bound to an ObservableCollection, which includes a DateTime field.


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


The GridView column for 'DateNR' is specified as follows:

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);


When I populate the collection from the database, DateNR can be null and is therefore not assigned to the collection property.

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);


Problem
The problem is that the ListView is displaying a date of "01-Jan-01" in the 'DateNR' column. 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' property in the collection to be a DateTime value and not a string because this column is used for sorting.

Any ideas would be welcome. thanks