Filter on different column to users selection

1 Answer 15 Views
Filter Grid
Rakhee
Top achievements
Rank 1
Iron
Iron
Iron
Rakhee asked on 26 Mar 2025, 11:15 AM | edited on 26 Mar 2025, 11:15 AM

I am using Visual Studio 2019 c#

I have a telerik radgrid which has a column named StageStDate which is in string format but shows a date in format dd-mmm-yy. This column has filtering enabled with a date picker.

I have a hidden column named StageStartDate which is a replicate of StageStDate but is a date column and in format DD/MM/YYYY.

As StageStDate wont filter correctly as its a string format, when user select a date for this column from filter I would like the filtering to apply to the StageStartDate column, how can I achieve this please?

On ItemCommand I have changed the Pair Second to StageStartDate instead of StageStDate

 

but for some reason this makes the filterExpression blank.

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 31 Mar 2025, 07:55 AM

Hi Rakhee,

Thank you for your detailed question.

You are right that StageStDate being a string column prevents proper filtering when using the date picker, as it leads to type mismatch issues. A great way to handle this is by redirecting the filter logic to your hidden DateTime column StageStartDate, while still allowing the user to interact with the string-based column.

There are two viable approaches for achieving this. I’ll outline both below so you can choose the one that best fits your scenario.

Approach 1: Replace the Filter Expression and Map to Real Date Column

In this approach, we intercept the filter command, extract the filter value, convert it to a valid DateTime, and build a safe FilterExpression targeting the DateTime column (StageStartDate).

    protected void rgMasterSchedule_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == Telerik.Web.UI.RadGrid.FilterCommandName)
        {
            Pair filterPair = (Pair)e.CommandArgument;
            string columnName = filterPair.Second.ToString();

            if (columnName == "StageStDate")
            {
                string originalExpression = rgMasterSchedule.MasterTableView.FilterExpression;

                if (!string.IsNullOrEmpty(originalExpression) && originalExpression.Contains("StageStDate"))
                {
                    string filterValue = filterPair.First.ToString();
                    DateTime parsedDate;

                    if (DateTime.TryParse(filterValue, out parsedDate))
                    {
                        // Format in a DateTime-parsable string (yyyy-MM-dd or yyyy/MM/dd)
                        string dateValue = parsedDate.ToString("yyyy-MM-dd");

                        // Replace the entire filter expression with one that uses the real date column
                        rgMasterSchedule.MasterTableView.FilterExpression = "([StageStartDate] = DateTime(" + parsedDate.Year + ", " + parsedDate.Month + ", " + parsedDate.Day + "))";

                        // Optional: clear the original column filter UI so it doesn’t show a ghost filter
                        foreach (GridColumn col in rgMasterSchedule.MasterTableView.Columns)
                        {
                            if (col.UniqueName == "StageStDate")
                            {
                                col.CurrentFilterFunction = GridKnownFunction.NoFilter;
                                col.CurrentFilterValue = string.Empty;
                            }
                        }

                        // rgMasterSchedule.Rebind();
                        e.Canceled = true;
                    }
                }
            }
        }
    }

    public class StageData
    {
        public int ID { get; set; }
        public string StageStDate { get; set; } // string version of date
        public DateTime StageStartDate { get; set; } // real date
    }

    protected void rgMasterSchedule_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        rgMasterSchedule.DataSource = GetDummyData();
    }

    private List<StageData> GetDummyData()
    {
        List<StageData> data = new List<StageData>();
        data.Add(new StageData
        {
            ID = 1,
            StageStartDate = new DateTime(2023, 12, 1),
            StageStDate = "01-Dec-23"
        });
        data.Add(new StageData
        {
            ID = 2,
            StageStartDate = new DateTime(2024, 1, 15),
            StageStDate = "15-Jan-24"
        });
        data.Add(new StageData
        {
            ID = 3,
            StageStartDate = new DateTime(2024, 3, 5),
            StageStDate = "05-Mar-24"
        });
        data.Add(new StageData
        {
            ID = 4,
            StageStartDate = new DateTime(2024, 3, 31),
            StageStDate = "31-Mar-24"
        });
        return data;
    }

This method keeps things simple and safe by avoiding type conversion errors.

 

Approach 2: Manually Handle Filtering (More Control)

If you prefer to bypass the built-in filtering logic entirely, you can construct and apply your own FilterExpression targeting the correct column based on parsed user input.


    protected void rgMasterSchedule_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == Telerik.Web.UI.RadGrid.FilterCommandName)
        {
            Pair filterPair = (Pair)e.CommandArgument;
            string columnName = filterPair.Second.ToString();

            if (columnName == "StageStDate")
            {
                string filterValue = filterPair.First.ToString();
                DateTime parsedDate;

                if (DateTime.TryParse(filterValue, out parsedDate))
                {
                    string formattedDate = parsedDate.ToString("yyyy-MM-dd");
                    rgMasterSchedule.MasterTableView.FilterExpression = "StageStartDate = \"" + formattedDate + "\"";

                    foreach (GridColumn col in rgMasterSchedule.MasterTableView.Columns)
                    {
                        col.CurrentFilterFunction = GridKnownFunction.NoFilter;
                        col.CurrentFilterValue = string.Empty;
                    }

                    // rgMasterSchedule.Rebind();
                    e.Canceled = true;
                }
            }
        }
    }

This method is useful when you want full control over custom logic, such as supporting ranges or advanced filter functions. 

For both approaches, it is important to cancel the default filtering (e.Canceled = true) to prevent RadGrid from applying the original (string-based) filter.

Here is my RadGrid declaration for both scenarios:

        <telerik:RadGrid ID="rgMasterSchedule" runat="server" AllowFilteringByColumn="true"
            AutoGenerateColumns="false" OnNeedDataSource="rgMasterSchedule_NeedDataSource"
            OnItemCommand="rgMasterSchedule_ItemCommand">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" />
                    
                    <telerik:GridBoundColumn DataField="StageStDate" HeaderText="Stage (String Date)"
                        UniqueName="StageStDate" AutoPostBackOnFilter="true" ShowFilterIcon="true"
                        CurrentFilterFunction="EqualTo" DataFormatString="{0:dd-MMM-yy}" />

                    <telerik:GridBoundColumn DataField="StageStartDate" HeaderText="StageStartDate"
                        UniqueName="StageStartDate" DataType="System.DateTime" Display="false" />
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>


Regards,
Rumen
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Tags
Filter Grid
Asked by
Rakhee
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Rumen
Telerik team
Share this question
or