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

GridDateTimeColumn filter format

3 Answers 687 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prasanth
Top achievements
Rank 1
Prasanth asked on 02 Feb 2012, 04:08 PM
Hi,

I need some help on date formatting in the Filter box for GridDateTimeColumn. I have 4 date columns in Telerik Grid. Because of space constriants
I don't want to show Datepicker to user, so that the user can enter date he chooses. The problem is when user enter date in
MMM/dd/yyyy format in filter box and press tab, the date is changing to format of MM/dd/yyyy.

i.e., Enter Dec/23/2011 in Posting Date filter field and press tab, then the the box shows 12/23/2011, but user wants to see same date format he entered.

Development code:

1. DataFormatString property is set programatically in Init event

RadGrid grd = (RadGrid)source;


            foreach (GridColumn col in grd.Columns)
            {
                if (col is GridDateTimeColumn)
                {
                    ((GridDateTimeColumn)col).DataFormatString = "{0:MMM/dd/yyyy"}";
                }
            }

2. ItemCreated event is used to set format of date

protected void grdRequests_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridFilteringItem)
        {
                RadDatePicker radPicker = ((GridFilteringItem)e.Item)["Posting Date"].Controls[0] as RadDatePicker;
                if(radPicker != null)
                {
                    radPicker.DateInput.DisplayDateFormat = "{0:MMM/dd/yyyy"}";
                    radPicker.DateInput.DateFormat = "{0:MMM/dd/yyyy"}";
                }
}
}

3. UI properties of Posting Date column
<telerik:GridDateTimeColumn HeaderText="Posting Date" HeaderButtonType="TextButton"
ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" DataField="Posting Date"
UniqueName="Posting Date" SortExpression="Order Date"
        AllowFiltering="true" ShowFilterIcon="true" DataType="System.DateTime" 
PickerType="None" AutoPostBackOnFilter="false" FilterControlWidth="35px">
<HeaderStyle Width="60px"/>
        <ItemStyle Wrap="false" />
</telerik:GridDateTimeColumn>

Note: This issue doesn't arise when I use PickerType="DatePicker". In this case even the filter text box looses focus 
the date format is not changing and still dispalys as Dec/23/2011.

Please provide your suggestion

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Feb 2012, 05:08 AM
Hello Prasanth,

Check the following help documentation which explains more about this.
Filtering for DateTime Bound Column with DataFormatString Different from mm/dd/yyyy.

-Shinu.
0
Prasanth
Top achievements
Rank 1
answered on 03 Feb 2012, 08:50 PM
Shinu,

Thanks you for your response. The read the link and it didn't address my problem. I don't have any issues in filtering date fields. I only have issue in displaying in the filter text box. Finally I resolved the issue by changing ItemCreated event code of Telerik Grid control

Instead of below code
if (e.Item is GridFilteringItem)
        {
                RadDatePicker radPicker = ((GridFilteringItem)e.Item)["Posting Date"].Controls[0] as RadDatePicker;
                if(radPicker != null)
                {
                    radPicker.DateInput.DisplayDateFormat = "{0:MMM/dd/yyyy"}";
                    radPicker.DateInput.DateFormat = "{0:MMM/dd/yyyy"}";
                }
}
} 

Replaced it with
if (e.Item is GridFilteringItem)
        {
                RadDateInput radpicker = ((GridFilteringItem)e.Item)["Posting Date"].Controls[1] as RadDateInput;
                if(radPicker != null)
                {
                    radPicker.DisplayDateFormat = "MMM/dd/yyyy";
                    radPicker.DateFormat = "MMM/dd/yyyy";
                }
}
} 

Thanks
Prasanth
0
Andrey
Telerik team
answered on 08 Feb 2012, 12:24 PM
Hi,

When PickerType is set to None, the RadDatePicker control is not rendered in the cell at all. Instead of the DatePicker, additional DateInput control is added to the cell. And that is why if you set the DateFormat of the DateInput control you won't notice the change, because the DateFormat is set to a different control.

You have found the right way with your last approach. Let us know if you have further questions.

Greetings,
Andrey
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Grid
Asked by
Prasanth
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Prasanth
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or