Handle Column Filter Server Side Events

Handling Server Side Event on Applying Column Filter

There are two main events as detailed below.

  1. OnBeforeColumnFilter: Fires before the filter is going to be applied on a column.
  2. OnAfterColumnFilter: Fires after the filter has been applied on a column.

Here is the ASPX script of the Aspose.Cells.GridWeb component to add and assign the aforementioned events.

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
<acw:GridWeb ID="GridWeb1" runat="server"
OnBeforeColumnFilter="GridWeb1_BeforeColumnFilter"
OnAfterColumnFilter="GridWeb1_AfterColumnFilter">
</acw:GridWeb>

These events can be used to get useful information about the filtering process such as column index and value on which filter has to be applied. Following is the snippet demonstrating the usage of OnBeforeColumnFilter event to retrieve the column index and value which user has selected on GridWeb UI for filtering.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
protected void GridWeb1_BeforeColumnFilter(object sender, RowColumnEventArgs e)
{
// Display the column index and filter applied
string msg = "[Column Index]: " + (e.Num) + ", [Filter Value]: " + e.Argument;
Label1.Text = msg;
}

On the other hand, if the requirement is to get number of filtered rows after the filter has been applied then you can use the OnAfterColumnFilter event as demonstrated below.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
protected void GridWeb1_AfterColumnFilter(object sender, RowColumnEventArgs e)
{
string hidden = "";
int headrow = 0;
int maxrow = GridWeb1.WorkSheets[0].Cells.MaxRow;
int count = 0;
// Iterate all worksheet rows to find out filtered rows
for (int i = headrow + 1; i <= maxrow; i++)
{
if (GridWeb1.WorkSheets[0].Cells.Rows[i].Hidden)
{
hidden += "-" + (i + 1);
}
else
{
count++;
}
}
// Display hidden rows and visible rows count
string msg = "[Hidden Rows]: " + hidden + " [Visible Rows]: " + count;
Label1.Text = msg;
}