format filter dropdown items of decimal and datetime fields in telerik:RadGrid.

1 Answer 26 Views
Filter Grid
Shebin
Top achievements
Rank 1
Shebin asked on 14 Feb 2024, 12:44 PM | edited on 16 Feb 2024, 10:34 AM

Hi,

Could you, please help me find code examples for how to format filter dropdown items of decimal and datetime fields(columns) in telerik:RadGrid.
We need a comma separator for decimal fields.  Also, there is a chance of negative values, so in that case, we need to show its absolute value(within parentheses).
In the case of datetime fields, we need only the date without the time part (date format will be different for different agencies).

We changed the format of Invoice Date column in  ItemDataBound event as below.

dataItem["Date"].Text = rowItem.Date.ToString(AgencyDateFormat);

But it only changed the grid column values, not the filter dropdown.(image of mentioned issue is given below)

We changed the format of Amount column in  ItemDataBound event as below.

dataItem["Amt"].Text = string.Format("{0:0,0.00;(0.00)}", rowItem.Amt);

But it only changed the grid column values, not the filter dropdown.(image of mentioned issue is given below)

 

What we need is for the values shown in the filter will be in the same format of the corresponding column.

I solved this issue by using following code:

private void RadGrid_GridFilterCheckListItemsRequested(object sender, GridFilterCheckListItemsRequestedEventArgs e)
        {
            string filterKey = e.Column.UniqueName;
            List<string> listOfItems = GetList(filterKey);

            foreach (var item in listOfItems)
            {
                if (e.Column.DataType == typeof(DateTime))
                {
                    DateTime datimeObj;
                    if (DateTime.TryParse(item, out datimeObj))
                    {
                        e.ListBox.Items.Add(new RadListBoxItem
                        {
                            Text = datimeObj.ToString(AgencyDateFormat),
                            Value = item
                        });
                        e.ListBox.DataTextFormatString = AgencyDateFormat;
                    }
                }
                else if (e.Column.DataType == typeof(decimal))
                {
                    decimal moneyObj;
                    if (decimal.TryParse(item, out moneyObj))
                    {
                        e.ListBox.Items.Add(new RadListBoxItem
                        {
                            Text = string.Format("{0:0,0.00;(0.00)}", moneyObj),
                            Value = item
                        });
                    }
                }
                else
                {
                    e.ListBox.DataSource = listOfItems;
                }
            }
            e.ListBox.DataBind();
        }

There is one more issue I'm facing. If the column contains a null or empty string, then I need to show them as "(Empty)" in the filter drop-down. How to do this?

We will appreciate your help.

Thanks

            

1 Answer, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 19 Feb 2024, 09:12 AM

Hi Shebian,

To show the null value as "(Empty)", you could adjust the shared code to check if the value is null or empty, and set the text value based on the result, like this: 

foreach (var item in listOfItems)
{
    string displayText = string.IsNullOrEmpty(item) ? "(Empty)" : item;  // Check if item is null or empty and replace it with "(Empty)"

    if (e.Column.DataType == typeof(DateTime))
    {
        DateTime datetimeObj;
        if (DateTime.TryParse(item, out datetimeObj))
        {
            e.ListBox.Items.Add(new RadListBoxItem
            {
                Text = datetimeObj.ToString(AgencyDateFormat),
                Value = displayText // Use displayText which will be "(Empty)" if item was null/empty
            });
        }
    }
}

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Tags
Filter Grid
Asked by
Shebin
Top achievements
Rank 1
Answers by
Vasko
Telerik team
Share this question
or