Telerik Grid Filter not working on Date column

后端 未结 2 946
無奈伤痛
無奈伤痛 2021-01-29 02:17

I have a Telerik Grid with one of the column having Date Time values, but the filter on that field is not working as the filter only accepts date by default. Is there any way we

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 02:58

    protected void ItemsRadGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.FilterCommandName)
            {
                try
                {
                    FillItemsGrid();
                    FilterByDate(ItemsRadGrid, e);
                }
                catch (Exception ex)
                {
                    //ExceptionLogClass.InsertError(ex);
                }
    
            }
    
        }
    
    public static void FilterByDate(RadGrid grid, GridCommandEventArgs e)
        {
            Pair filterPair = e.CommandArgument as Pair;
            string columnName = Convert.ToString(filterPair.Second);
            if (filterPair.First.ToString() == "NoFilter")
            { }
            else
            {
                if (grid.Columns.FindByDataField(columnName).DataType.Name == "DateTime")
                {
                    try
                    {
                        TextBox FilterColumnField = ((TextBox)((GridFilteringItem)e.Item)[columnName].Controls[0]);
                        string oldDate = FilterColumnField.Text;
                        FilterColumnField.Text = DateTime.ParseExact(FilterColumnField.Text, "dd.MM.yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy 12:00:00 tt");
                    }
                    catch (FormatException)
                    {
                        e.Canceled = true;
                    }
                }
            }
        }
    

提交回复
热议问题