AUTHOR: Marin Bratanov
DATE POSTED: December 06, 2017
Show an indicator that a column (field) is filtered when Excel-like filtering is used.
You can use the PreRender event of the grid, loop through the columns and see which column has a filter expression. If it has, access its cell and apply changes (class name, attributes, etc.).
For example:
//show filtering indicator
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
GridHeaderItem header = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0]
as
GridHeaderItem;
foreach
(GridColumn col
in
RadGrid1.MasterTableView.RenderColumns
.OfType<IGridDataColumn>().Where(x => x.AllowFiltering))
if
(!
string
.IsNullOrEmpty(col.EvaluateFilterExpression()))
TableCell cell = header[col.UniqueName];
//style the cell as desired (e.g., change class, background color, add image, etc.
cell.CssClass =
"myFilteredColumnHeader"
;
cell.BackColor = System.Drawing.Color.Aqua;
cell.Style[
"background-image"
] =
"none"
cell.Controls.Add(
new
Image()
ID =
"FilterIndicator"
+ col.UniqueName,
ImageUrl =
"~/images/filterIndicator.png"
});
}
//data binding
private
class
DataRowComparer : IEqualityComparer<DataRow>
dataField;
public
DataRowComparer(
dataField)
this
.dataField = dataField;
bool
Equals(DataRow x, DataRow y)
return
x[dataField].ToString() == y[dataField].ToString();
int
GetHashCode(DataRow dataRow)
dataRow[dataField].GetHashCode();
Page_Load(
RadGrid1_NeedDataSource(
sender, GridNeedDataSourceEventArgs e)
RadGrid1.DataSource = GetGridSource();
DataTable GetGridSource()
DataTable dataTable =
DataTable();
DataColumn column =
DataColumn();
column.DataType = Type.GetType(
"System.Int32"
);
column.ColumnName =
"OrderID"
dataTable.Columns.Add(column);
column =
"System.Decimal"
"Freight"
"System.String"
"ShipName"
"ShipCountry"
DataColumn[] PrimaryKeyColumns =
DataColumn[1];
PrimaryKeyColumns[0] = dataTable.Columns[
];
dataTable.PrimaryKey = PrimaryKeyColumns;
for
(
i = 0; i <= 80; i++)
DataRow row = dataTable.NewRow();
row[
] = i + 1;
] = (i + 1) + (i + 1) * 0.1 + (i + 1) * 0.01;
"Name "
+ (i % 6 + 1);
"Country "
+ (i % 9 + 1);
dataTable.Rows.Add(row);
dataTable;
DataTable GetListBoxSource(
DataTable table = GetGridSource().Clone();
table.Rows.Clear();
GetGridSource().Rows.Cast<DataRow>().Distinct<DataRow>(
DataRowComparer(dataField))
.ToList().ForEach(x => table.ImportRow(x));
table;
RadGrid1_FilterCheckListItemsRequested(
sender, GridFilterCheckListItemsRequestedEventArgs e)
DataField = (e.Column
IGridDataColumn).GetActiveDataField();
e.ListBox.DataSource = GetListBoxSource(DataField);
e.ListBox.DataKeyField = DataField;
e.ListBox.DataTextField = DataField;
e.ListBox.DataValueField = DataField;
e.ListBox.DataBind();
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
"server"
AllowPaging
"True"
CellSpacing
"0"
GridLines
"None"
Width
"800px"
OnNeedDataSource
"RadGrid1_NeedDataSource"
AllowFilteringByColumn
"true"
FilterType
"HeaderContext"
EnableHeaderContextMenu
EnableHeaderContextFilterMenu
OnFilterCheckListItemsRequested
"RadGrid1_FilterCheckListItemsRequested"
OnPreRender
"RadGrid1_PreRender"
RenderMode
"Lightweight"
>
MasterTableView
AutoGenerateColumns
"False"
DataKeyNames
Columns
telerik:GridBoundColumn
DataField
DataType
FilterControlAltText
"Filter OrderID column"
HeaderText
ReadOnly
SortExpression
UniqueName
FilterCheckListEnableLoadOnDemand
</
telerik:GridNumericColumn
"Filter Freight column"
AllowFiltering
"false"
"Filter ShipName column"
"Filter ShipCountry column"
Resources Buy Try