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

DateTime filtering date only with custom format

11 Answers 715 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 08 Jan 2016, 10:09 AM

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

Sort by
0
Stefan Nenchev
Telerik team
answered on 13 Jan 2016, 08:52 AM
Hi Thomas,

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
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 Feedback Portal and vote to affect the priority of the items
0
Thomas
Top achievements
Rank 1
answered on 13 Jan 2016, 09:53 AM

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

0
Stefan Nenchev
Telerik team
answered on 13 Jan 2016, 02:42 PM
Hi 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
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 Feedback Portal and vote to affect the priority of the items
0
Thomas
Top achievements
Rank 1
answered on 14 Jan 2016, 07:52 AM

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

0
Stefan Nenchev
Telerik team
answered on 14 Jan 2016, 02:01 PM
Hi 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
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 Feedback Portal and vote to affect the priority of the items
0
Thomas
Top achievements
Rank 1
answered on 14 Jan 2016, 03:17 PM

Hi Stefan,

yes, that does it.

Many thanks for your support,

Best regards,

   Thomas

0
Thomas
Top achievements
Rank 1
answered on 15 Jan 2016, 07:25 AM

Hi Stefan,

your approach work fine for the filtering, but will break the sorting as we now have not bind the times :-(

Regards,

  Thomas

0
Stefan Nenchev
Telerik team
answered on 15 Jan 2016, 01:53 PM
Hello 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
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 Feedback Portal and vote to affect the priority of the items
0
Thomas
Top achievements
Rank 1
answered on 18 Jan 2016, 08:26 AM

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

0
Stefan Nenchev
Telerik team
answered on 19 Jan 2016, 02:59 PM
Hello 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
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 Feedback Portal and vote to affect the priority of the items
0
Thomas
Top achievements
Rank 1
answered on 19 Jan 2016, 03:19 PM

Ups,

I have to set the SortMemberPath to "StartTime" - grrr - now it works.

Cheers,

  Thomas

Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Thomas
Top achievements
Rank 1
Share this question
or