Simple way to Show/Hide Filter in RadGrid


Description:

Telerik’s RadGrid provides in-built search functionality. You can find the search functionality at top of the grid just below the header columns. But user should be able to hide the same to save unnecessary space all the time from the grid.

RadGrid Example when Filter is not visible to the user:

Radgrid without Filter

Radgrid without Filter

RadGrid Example when Filter is visible to the user:

RadGrid With Filter

RadGrid With Filter


Code:

First of all in RadGrid’s MasterTableView tag, add following checkbox:

<MasterTableView Width=”100%” CommandItemDisplay=”Top” GroupLoadMode=”Server”>

<CommandItemTemplate>

<asp:CheckBox ID=”CheckBox2″ AutoPostBack=”true” CssClass=”CheckBox” runat=”server”

ForeColor=”White” Text=”Show Filter” />

</CommandItemTemplate>

After that add the ItemCreated event in the RadGrid as following code does:

protected void grdManageTransType_ItemCreated(object sender, GridItemEventArgs e)

{

if (e.Item.ItemType == GridItemType.CommandItem)

{

GridCommandItem commandItem = e.Item as GridCommandItem;

CheckBox chkFilter2 = (CheckBox)commandItem.FindControl(“CheckBox2”);

chkFilter2.AutoPostBack = true;

chkFilter2.ForeColor = System.Drawing.Color.White;

chkFilter2.Checked = grdManageTransType.AllowFilteringByColumn;

//Adding the event handler

chkFilter2.CheckedChanged += new EventHandler(chkFilter2_CheckedChanged);

}

if (e.Item.ItemType == GridItemType.SelectedItem)

e.Item.BackColor = Color.FromName(“#FFEDC1”);

}

In above portion of code we are creating CheckedChanged event of the checkbox. So now create the CheckedChanged event of the checkbox as per following code:

protected void chkFilter2_CheckedChanged(object sender, EventArgs e)

{

grdManageTransType.AllowFilteringByColumn = !grdManageTransType.AllowFilteringByColumn;

grdManageTransType.Rebind();

}

As per the above code portions, we can the checkbox at the top of the RadGrid. On click of that checkbox RadGrid’s filter should be visible on or off.