Hi,
I have DateTime columns in my gridview.
I will see the full date and time values with a custom format string yyyy-MM-dd HH:mm:ss.
But I only want to filter for the date only, so I set the FilterMemberPath to the date value.
<telerik:GridViewDataColumn Header="Start-Date"
IsCustomSortingEnabled="False"
DataMemberBinding="{Binding StartTime}"
FilterMemberPath="StartTime.Value.Date"
DataFormatString="{} {0:yyyy-MM-dd HH:mm:ss}" />
I'm now able to filter for a specific date with equal to,
but in the selection list of containing entries the dates will be displayed with 12:00:00 AM time :-(
How could I change the format of the filter string to yyyy-MM-dd?
Many thanks for all help.
Cheers,
Thomas
11 Answers, 1 is accepted
Indeed, the default behavior of the Filtering control is to add a value for hours, minutes and seconds of the bound DateTime property. Unfortunately, there is no straightforward approach for achieving the desired behavior.
An approach you could take is to work with the filtering events of the RadGridView and change the DataFormatString property. You can subscribe for the FilterOperatorsLoading event, which is fired every time the filtering control is opened. Eventually, use the Filtered event to switch the FormatString to the original one. This, however, would work only if some filtering has been applied and could be used in specific scenarios.
Regards,
Stefan Nenchev
Telerik
Hi Stefan,
is it possible to get a demo Project from your side how I have to work with the events in the filter control?
Or might it be easier to use a custom filter control?
Best regards,
Thomas
I have investigated the issue further and managed to come up with a better approach. It includes changing the CellTemplate of the specific column. Please refer to the following code snippet:
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding Established.Date}"
Header
=
"Est."
DataFormatString
=
"{}{0:yyyy-MM-dd}"
>
<
telerik:GridViewDataColumn.CellTemplate
>
<
DataTemplate
>
<
TextBlock
Text
=
"{Binding Established, StringFormat={}{0:yyyy-MM-dd hh:mm:ss}}"
/>
</
DataTemplate
>
</
telerik:GridViewDataColumn.CellTemplate
>
</
telerik:GridViewDataColumn
>
I have also attached a sample project implementing the suggested approach.
Regards,
Stefan Nenchev
Telerik
Hi Stefan,
this feels much better :-)
As I have a nullable DateTime property I have to Change the DataMemberBinding to StartTime.Value.Date as I will loose the filtering Icon in other case.
One Problem still exists. In the two fields of the date selection I still have a different Format. This field seems not to follow the DataFormatString :-(
Is there a way too change this as well?
Many thanks,
Thomas
You need to change the CurrentCulture of your application. The RadDateTimePicker uses that Culture in order to parse the input date and time.
Please try the following approach:
InitializeComponent();
var culture =
new
CultureInfo(
"en-US"
);
var dtfInfo =
new
DateTimeFormatInfo
{
ShortDatePattern =
"yyyy-MM-dd"
,
ShortTimePattern =
"HH:mm"
,
DateSeparator =
"-"
};
culture.DateTimeFormat = dtfInfo;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
You can find more information at the following article from our documentation page - How to Set Different Culture.
I have tested this approach and it seems to do the job. Please update me if everything is working as expected at your end as well.
Regards,
Stefan Nenchev
Telerik
Hi Stefan,
yes, that does it.
Many thanks for your support,
Best regards,
Thomas
Hi Stefan,
your approach work fine for the filtering, but will break the sorting as we now have not bind the times :-(
Regards,
Thomas
Setting the SortMemberPath property of the column should do the job for you:
SortMemberPath="Established"
I have tested it and it is working as expected, as you can see in this video. Please try this at your end and update me.
Regards,
Stefan Nenchev
Telerik
Hi Stefan,
I tried to set SortMemberPath="Established", but in my case this seems to disable sorting completly.
Could it be a Problem that my StartTime is nullable?
Thanks,
Thomas
In the video recorded I have set a nullable DateTime property and I am able to perform the sorting. I have attached the sample project once again so you can review it. Would it be possible for you to update this project so that it represents your setup? Eventually, you can create a ticket and send it over so I can have a more detailed look.
Regards,
Stefan Nenchev
Telerik
Ups,
I have to set the SortMemberPath to "StartTime" - grrr - now it works.
Cheers,
Thomas