RadGrid for ASP.NET

Filtering for built-in checkbox column Send comments on this topic.
Filtering > How to > Filtering for built-in checkbox column

Glossary Item Box

When you want to display separate filtering options for built-in GridCheckBoxColumn a suitable solution might be to remove the default filter text box and image and put a dropdown list in the column header (with ClearFilter/Show all checked/Show all unchecked options). Thus you can filter the grid records according to the chosen condition and still keep the default filtering menu for the rest of the grid columns.

To replace the default textbox filter for GridCheckBoxColumn you can wire the ItemCreated event of the grid and modify the Controls collection of the corresponding header cell. You also need to wire the SelectedIndexChanged event of the dropdown list (which will be in the place of the textbox) in order to modify the grid FilterExpression in par with the SelectedValue of he combobox.
Finally, to persist the filter criteria for the checkbox column when you trigger filter action from other column, hook the NeedDataSource event and add the necessary pattern to the FilterExpression.

Below are some code snippets showing the approach:

ASPX/ASCX Copy Code
<rad:RadGrid ID="RadGrid1" AllowSorting="True" AllowPaging="true" EnableAJAX="true" ShowStatusBar="true"
               
Skin= "Windows" runat="server" GridLines="None" Width="300px" AllowFilteringByColumn="true">
               
<PagerStyle Mode="NextPrevAndNumeric" />
               
<MasterTableView AutoGenerateColumns="false">
                   
<Columns>
                       
<rad:GridCheckBoxColumn UniqueName="GridCheckBoxColumn" DataField="Bool" HeaderText="Check/UnCheck" />
                       
<rad:GridBoundColumn UniqueName="ContactName" HeaderText="Contact name" DataField="ContactName" />
                   
</Columns>
               
</MasterTableView>
               
<PagerStyle Mode="NumericPages" />
</
rad:RadGrid>
C# Copy Code
string connectionString = ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath( "~/Grid/Data/Access/Nwind.mdb"));


   
public DataTable GetDataTable(string query)
   {
       OleDbConnection connection1 =
new OleDbConnection(connectionString);
       OleDbDataAdapter adapter1 =
new OleDbDataAdapter();
       adapter1.SelectCommand =
new OleDbCommand(query, connection1);
       DataTable table1 =
new DataTable();
       connection1. Open();
       
try
      {
           
adapter1.Fill(table1);
       }
       
finally
       {
           
connection1. Close();
       }
        
return table1;
   }

   
protected void RadGrid1_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e)
   {
        
if (e.Item is GridFilteringItem)
       {
           GridFilteringItem filteringItem = (e.Item
as GridFilteringItem);
           filteringItem[
"GridCheckBoxColumn"].Controls.Clear();

           DropDownList ddList =
new DropDownList();
           ddList.AutoPostBack = true;
           ddList.SelectedIndexChanged +=
new System.EventHandler(this.ddList_SelectedIndexChanged);

           ddList.Items.Add(
new ListItem("Clear filter", "Empty"));
           ddList.Items.Add(
new ListItem("Show all checked", "True"));
           ddList.Items.Add(
new ListItem("Show all unchecked", "False"));

            
if (Session["ddlSelValue"] != null)
           {
               ddList.Items.FindByValue((
string)Session("ddlSelValue")).Selected = true;
           }
           filteringItem[
"GridCheckBoxColumn"].Controls.Add(ddList);
       }
   }

   
protected void ddList_SelectedIndexChanged(object sender, System.EventArgs e)
   {
       DropDownList ddList = (DropDownList)sender;
       Session[
"ddlSelValue"] = ddList.SelectedValue;

       
switch (ddList.SelectedValue)
       {
            
case "True":
               RadGrid1.MasterTableView.FilterExpression =
"([Bool] = True) ";

               
foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
               {
                   column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                   column.CurrentFilterValue = String.Empty;
               }
               RadGrid1.MasterTableView.Rebind();
               
break;
            
case "False":
               RadGrid1.MasterTableView.FilterExpression =
"([Bool] = False) ";

               
foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
               {
                   column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                   column.CurrentFilterValue = String.Empty;
               }
               RadGrid1.MasterTableView.Rebind();
               
break;
            
case "Empty":
               RadGrid1.MasterTableView.FilterExpression = String.Empty;

               
foreach (GridColumn column in RadGrid1.MasterTableView.Columns)
               {
                   column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                   column.CurrentFilterValue = String.Empty;
               }
               RadGrid1.MasterTableView.Rebind();
               
break;
       }
   }

   
protected void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
   {
       RadGrid1.DataSource = GetDataTable(
"Select ContactName, Bool FROM Customers");

        
if (Session["ddlSelValue"] != null)
       {
           RadGrid1.MasterTableView.FilterExpression = RadGrid1.MasterTableView.FilterExpression +
"AND ([Bool] = "
           
+ (string)Session("ddlSelValue") + ")";
       }
   }


 

VB.NET Copy Code
Public Shared connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath( "~/Grid/Data/Access/Nwind.mdb")

    Public Function GetDataTable(ByVal query As String) As DataTable
         Dim connection1 As New OleDbConnection(connectionString)
         Dim adapter1 As New OleDbDataAdapter
        adapter1.SelectCommand = New OleDbCommand(query, connection1)
         Dim table1 As New DataTable
        connection1. Open()
        Try
            adapter1.Fill(table1)
        Finally
            connection1. Close()
         End Try
         Return table1
    End Function

    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemCreated
         If (TypeOf e.Item Is GridFilteringItem) Then
             Dim filteringItem As GridFilteringItem = CType(e.Item, GridFilteringItem)

            filteringItem( "GridCheckBoxColumn").Controls.Clear()

             Dim ddList As DropDownList = New DropDownList()
            ddList.AutoPostBack = True

            AddHandler ddList.SelectedIndexChanged, AddressOf ddList_SelectedIndexChanged

            ddList.Items.Add( New ListItem("Clear filter", "Empty"))
            ddList.Items.Add( New ListItem("Show all checked", "True"))
            ddList.Items.Add( New ListItem("Show all unchecked", "False"))

             If (Not Session("ddlSelValue") Is Nothing) Then
                ddList.Items.FindByValue(CType(Session( "ddlSelValue"), String)).Selected = True
             End If

            filteringItem( "GridCheckBoxColumn").Controls.Add(ddList)
         End If
    End Sub
    Protected Sub ddList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
         Dim ddList As DropDownList = CType(sender, DropDownList)

        Session( "ddlSelValue") = ddList.SelectedValue
         Select Case ddList.SelectedValue
             Case "True"
                RadGrid1.MasterTableView.FilterExpression = "([Bool] = True) "

                 For Each column As GridColumn In RadGrid1.MasterTableView.Columns
                    column.CurrentFilterFunction = GridKnownFunction.NoFilter
                    column.CurrentFilterValue = String.Empty
                 Next

                RadGrid1.MasterTableView.Rebind()
             Case "False"
                RadGrid1.MasterTableView.FilterExpression = "([Bool] = False) "

                 For Each column As GridColumn In RadGrid1.MasterTableView.Columns
                    column.CurrentFilterFunction = GridKnownFunction.NoFilter
                    column.CurrentFilterValue = String.Empty
                 Next

                RadGrid1.MasterTableView.Rebind()
             Case "Empty"
                RadGrid1.MasterTableView.FilterExpression = String.Empty

                 For Each column As GridColumn In RadGrid1.MasterTableView.Columns
                    column.CurrentFilterFunction = GridKnownFunction.NoFilter
                    column.CurrentFilterValue = String.Empty
                 Next

                RadGrid1.MasterTableView.Rebind()
         End Select

    End Sub

Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        RadGrid1.DataSource = GetDataTable("Select ContactName, Bool FROM Customers")

         If (Not Session("ddlSelValue") Is Nothing) Then
            RadGrid1.MasterTableView.FilterExpression = RadGrid1.MasterTableView.FilterExpression & "AND ([Bool] = " & CType(Session("ddlSelValue"), String) & ")"
         End If
End Sub