customize the filter option for a date colmn telerik grid

karthik k picture karthik k · Dec 3, 2010 · Viewed 8.2k times · Source

I am using telerik grid. I need to apply a filter for all the columns in my grid. Currenly I am customizing the filter option using the following code. By using the following code, I am removing the certain items for all the columns. But, for a date column could any one please tell me what are the possible options for filtering in the grid and how to customize those filtering options?

Code Behind

    protected void RGVTest_Init(object sender, EventArgs e)
    {

        GridFilterMenu menu = RGVTest.FilterMenu;
        int i = 0;
        while (i < menu.Items.Count)
        {
            if (menu.Items[i].Text == "Between" ||
                menu.Items[i].Text == "NotBetween")
            {
                menu.Items.RemoveAt(i);
            }
            else
            {
                i++;
            }
        }
    }

*Aspx:*

<telerik:RadGrid ID="RGVTest" runat="server" Skin="Vista" AllowPaging="True"
            AllowFilteringByColumn="true" AllowSorting="true" GridLines="None" OnItemCommand="RGVTest_ItemCommand"
            PageSize="10" OnNeedDataSource="RGVTest_NeedDataSource" OnItemDataBound="RGVTest_ItemDataBound"
            OnInit="RGVTest_Init">
            <GroupingSettings CaseSensitive="false" />
            <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" />
            <MasterTableView AutoGenerateColumns="False" CellSpacing="-1" >
                <NoRecordsTemplate>
                    <div style="color: red">
                        No Records to display!
                    </div>
                </NoRecordsTemplate>
                <Columns>

                    <telerik:GridTemplateColumn DataField="SSN" ReadOnly="True" HeaderText="SSN" UniqueName="SSN"
                        SortExpression="SSN">
                        <ItemTemplate>
                            <asp:Label ID="LblSSN" runat="server" Text='<%#Eval("SSN") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Left" Width="5%" />
                    </telerik:GridTemplateColumn>

                    <telerik:GridTemplateColumn DataField="Date" HeaderText="Date" UniqueName="Date"
                        SortExpression="Date">
                        <ItemTemplate>
                            <asp:Label ID="LblDate" runat="server" Text='<%#Eval("Date","{0:MM/dd/yyyy}") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Left" Width="4%" />
                    </telerik:GridTemplateColumn>

                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

Answer

Suresh Chaudhary picture Suresh Chaudhary · Jan 28, 2011

you do like this way:

 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
 {  
     if (e.CommandName == RadGrid.FilterCommandName)  
     {  
         Pair filterPair = e.CommandArgument as Pair;  
         string columnName = Convert.ToString(filterPair.Second);  
         if (columnName == "CreationDate")  
         {  
            e.Canceled = true;  
             string date = ((TextBox)((GridFilteringItem)e.Item)[Convert.ToString(filterPair.Second)].Controls          [0]).Text;  
             DateTime startDate = Convert.ToDateTime(date);  
             DateTime endDate = startDate.AddDays(1);  
             string newFilter = "('" + startDate.ToString("MM/dd/yyyy") + "' <= [CreationDate] AND [CreationDate] <= '"         + endDate.ToString("MM/dd/yyyy") + "')";   
             GridBoundColumn dateColumn = (GridBoundColumn)e.Item.OwnerTableView.GetColumnSafe(columnName);  
             dateColumn.CurrentFilterValue = startDate.ToString("MM/dd/yyyy");  
            RadGrid1.MasterTableView.FilterExpression = newFilter;  
             RadGrid1.Rebind();  
         }  
     }  
 }