This is a migrated thread and some comments may be shown as answers.

Complications binding GridViewDataColumn to Nullable<DateTime>

6 Answers 347 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James Craig
Top achievements
Rank 1
James Craig asked on 22 Jul 2010, 03:38 PM

Good Day!

I wish to bind a Nullable<DateTime> property to a grid column; further, I wish to display and sort by date/time, but group by date alone & also have discrete date values only appear in the filter dialog.  I'm using the following test code:

XAML:

 <DataTemplate x:Key="ctd1">
   <TextBlock Text="{Binding d1, Converter={StaticResource bConvDateTimeToCultureString}}" />
 </DataTemplate>
 ...
 <telerik:RadGridView Name="radGridView1">
 <telerik:RadGridView.Columns>
  <!--<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=d1.Value.Date}" CellTemplate="{StaticResource ctd1}" /> -->
  <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=d1}" GroupMemberPath="d1.Value.Date" />
  <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=d2}" GroupMemberPath="d1.Value.Date" />
 </telerik:RadGridView.Columns>
</telerik:RadGridView>

Code:

public class DatePair
{
 public DateTime? d1 { get; set; }
 public DateTime? d2 { get; set; }
 public DatePair(int n)
 {
  d1 = DateTime.Now.AddDays(n);
  d2 = DateTime.Now.AddMinutes(n + 10);
 }
}

radGridView1.ItemsSource = new DatePair[] { new DatePair(12), new DatePair(13), new DatePair(14) };

Approach 1:

<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=d1}" GroupMemberPath="d1.Value.Date" />

Problems:
Property group headers are formatted using the invariant culture
Filter dialog lists discrete values for each date/time.  I want users to be able to select by discrete date (how can I control this?).  Filter dialog also displays its dates according to the invariant culture (why this switch?).

Approach 2:

<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=d1.Date}" CellTemplate="{StaticResource ctd1}" SortMemberPath="d1" />

Problems:

N.B. the CellTemplate's converter ensures that the date is formatted using the current culture (without this it's formatted using the invariant culture).
Can no longer perform grouping, grid runs poorly, but column is populated and sorts correctly.

Approach 3:

<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=d1.Value.Date}" CellTemplate="{StaticResource ctd1}" SortMemberPath="d2" />

Problems:
Grouping is done by date, and group headers are formatted using the current culture (exactly what I want).
However, the filter dialog show dates in the invariant culture.
Binding errors like that below are raised every time rows are generated, causing an appreciable slowdown.

BindingExpression path error: 'Value' property not found on '05/08/2010 15:21:18' 'System.DateTime' (HashCode=-196747520). BindingExpression: Path='d1.Value.Date' DataItem='SilverlightTestApp.MainPage+DatePair' (HashCode=36615679); target element is 'Telerik.Windows.Controls.GridView.GridViewCell' (Name=''); target property is 'Value' (type 'System.Object').

Please can someone advise as to the correct way to handle this scenario?

Many thanks,

James

6 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 22 Jul 2010, 05:52 PM
Hello James Craig,

 I would suggest to go with the third approach. You only need to change the path on your binding from "d1.Value.Date" to "d1.Date" to take care of those exceptions. Even though the property is of a nullable type, the Silverlight runtime provides the actual type as a value, so you get DateTime, instead of Nullable<DateTime>. Also, you don't need to set a CellTemplate to format the DateTime string. Just set the DataFormatString property of the column to whatever format you need. This will also reflect in the Filtering list.

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
James Craig
Top achievements
Rank 1
answered on 22 Jul 2010, 06:19 PM
Hello Yavor,

Many thanks for your reply.  Using my test code below, if I bind to 'd1.Date' the column becomes unsortable and unfilterable.  If I then set GroupMemberPath="d1.Date", the result is the same; if I change this to GroupMemberPath="d1.Value.Date", then I can group but still can't filter.  Furthermore, the group headings are formatted according to the wrong culture (and setting DataFormatString doesn't fix this).  This can be reproduced by running the example I pasted into the first post.  It seems there are two issues -1) the value of nullable objects isn't resolved correctly in some cases; 2) under certain circumstances, when binding to DateTime?, the culture info used to display dates switches, and I can't figure out why it should!

Best regards,

James
0
Yavor Georgiev
Telerik team
answered on 23 Jul 2010, 01:00 PM
Hello James Craig,

 We have determined that there is indeed an issue when the RadGridView is employed in your scenario. I have opened a couple of PITS issues, which you can find under Your Tracked Issues once you login to your telerik account. I have also awarded you Telerik Points. We will contact you as soon as there is some development on these issues.

Sincerely yours,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
James Craig
Top achievements
Rank 1
answered on 23 Jul 2010, 05:12 PM
Hello Yavor,

Many thanks indeed for looking into this.  Hopefully a fix will arrive in the near future!

Best regards,
James
0
Accepted
Yavor Georgiev
Telerik team
answered on 26 Jul 2010, 01:47 PM
Hi James Craig,

 We have ascertained that most of the issues you are facing are Silverlight problems. The Culture formatting problem is resolved very easily by setting the Language property of the ContentControl containing the RadGridView.

The BindingExpression errors are still to be blamed on the Framework, as Microsoft's DataGrid control behaves in much the same way. The real issue right now with our controls is that grouping and filtering does not work property when DataMemberBinding is 'd1.Date', and not 'd1.Value.Date'. We will continue investigating this. The work-around for this is to use a cell template.

I'm attaching a sample project. Please let me know if there's anything else.

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
James Craig
Top achievements
Rank 1
answered on 02 Aug 2010, 11:07 AM
Hi Yavor,

Apologies for the lateness of my reply - I've been away for the past week.  It does indeed appear that most of these issues (everything except the issues that arise when binding to 'nDt.Date') can be attributed to the framework.  Many thanks indeed for your invaluable assistance!

Best regards,

James
Tags
GridView
Asked by
James Craig
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
James Craig
Top achievements
Rank 1
Share this question
or