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