DateTime在绑定到DataGrid后显示随机日期

时间:2014-04-28 23:03:53

标签: c# wpf nullable

我有ObservableCollection<object>我绑定到代码后面的DataGrid,我添加了列并为后面的代码中的那些列生成绑定。

   private void RgvReportDisplay_Loaded( object sender, RoutedEventArgs e )
        {
            if (RgvReportDisplay.Columns.Count == 0)
            {
                foreach (var item in ((ReportsQueryHolder)DataContext).Collection[0].GetType().GetProperties())
                {
                    Binding B = null;

                    if (item.Name.ToLower().Contains("date") || item.Name.ToLower().Contains("scheduledpickup"))
                    {
                        B = new Binding(item.Name + ".Date");
                        //B.Converter = new DateConverter();
                        B.Mode = BindingMode.OneWay;
                    }
                    else
                    {
                        B = new Binding(item.Name);
                        B.Mode = BindingMode.OneWay;
                    }

                    RgvReportDisplay.Columns.Add(new Microsoft.Windows.Controls.DataGridTextColumn()
                    {
                        Header = item.Name,
                        Binding = B 
                    });      
                }
            }
        }

当我在绑定之前查看Collection列表时,日期看起来是正确的。以下是我的立即窗口中的内容:

((ReportsQueryHolder)DataContext).Collection[0]
{DataTableType}
    Amount: "749.00"
    Collector: "John Smith"
    CreatedDate: {3/11/2014 8:00:39 PM}
    LocationName: "QUICK STOP "
    LocationNumber: "37"
    ScheduledPickup: {3/12/2014 12:00:00 AM}

我的日期搞砸了。我的约会现在说它被绑定到DataGrid时的随机事物。这里显示了什么:

     10/12/8524

所以有些如何将3/12/2014转换为10/12 / 8524.这曾经有效,但现在没有。

已添加:

事实证明,我绑定了nullable DateTime,这导致了问题。为什么声明这些字段DateTime?导致这个?

1 个答案:

答案 0 :(得分:0)

我最终在我的日期转换器中处理null,因为NULL显示为DateTime.MinValue因此我只是检查了它然后根据它返回了一个字符串