RadGrid for ASP.NET

Applying default filter on initial load Send comments on this topic.
Filtering > How to > Applying default filter on initial load

Glossary Item Box

A common scenario is to apply default filter for the grid when the page in which the control resides is loaded in the browser. This can be achieved quite easily using the FilterExpression attribute of the grid (more about the filter expression you can learn from this topic) and the CurrentFilterFunction/CurrentFilterValue for the corresponding grid column. The actions for modifying these properties can take place inside the NeedDataSource handler inside !Page.IsPostBack conditional block.
The forthcoming code sample demonstrates this technique:

ASPX/ASCX Copy Code
  <rad:RadGrid id="RadGrid1" runat="server" AllowFilteringByColumn="true" Width="500px" AllowPaging="true"
   
PageSize="15">
   
<PagerStyle Mode="NumericPages" />
   
<MasterTableView AutoGenerateColumns="false" AllowPaging="true" PageSize="15">
    
<Columns>
     
<rad:GridBoundColumn UniqueName="ContactName" HeaderText="Contact name" DataField="ContactName" AllowFiltering="false" />
     
<rad:GridBoundColumn UniqueName="City" HeaderText="City" DataField="City" />
     
<rad:GridBoundColumn UniqueName="PostalCode" HeaderText="Postal code" DataField="PostalCode" />
     
<rad:GridBoundColumn UniqueName="Country" HeaderText="Country" DataField="Country" />
    
</Columns>
   
</MasterTableView>
  
</rad:RadGrid>

 

VB.NET Copy Code
Private Sub RadGrid1_NeedDataSource(ByVal source As System.Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        Dim MyOleDbConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath
        ("~/Grid/Data/Access/Nwind.mdb"))
        Dim MyOleDbDataAdapter As New OleDbDataAdapter
        MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT ContactName, Country, City, PostalCode FROM Customers",
        MyOleDbConnection)
        Dim myDataTable As New DataTable
        MyOleDbConnection.Open()
        Try
            MyOleDbDataAdapter.Fill(myDataTable)
        Finally
            MyOleDbConnection.Close()
        End Try
        RadGrid1.DataSource = myDataTable.DefaultView
        If (Not Page.IsPostBack) Then
            RadGrid1.MasterTableView.FilterExpression = "([Country] LIKE '%Germany%') "
            Dim column As GridColumn = RadGrid1.MasterTableView.GetColumnSafe("Country")
            column.CurrentFilterFunction = GridKnownFunction.Contains
            column.CurrentFilterValue = "Germany"
        End If
End Sub

 

C# Copy Code
private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
{
       OleDbConnection MyOleDbConnection =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath
       (
"~/Grid/Data/Access/Nwind.mdb"));
       OleDbDataAdapter MyOleDbDataAdapter =
new OleDbDataAdapter();
       MyOleDbDataAdapter.SelectCommand =
new OleDbCommand("SELECT ContactName, Country, City, PostalCode FROM Customers",
       MyOleDbConnection);

       DataTable myDataTable =
new DataTable();
       MyOleDbConnection.Open();

       
try
       {
           
MyOleDbDataAdapter.Fill(myDataTable);
       }
       
finally
       {
           
MyOleDbConnection.Close();
       }

       RadGrid1.DataSource = myDataTable.DefaultView;

       
if (!Page.IsPostBack)
       {
           RadGrid1.MasterTableView.FilterExpression =
"([Country] LIKE \'%Germany%\') ";

           GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(
"Country");
           column.CurrentFilterFunction = GridKnownFunction.Contains;
           column.CurrentFilterValue =
"Germany";
       }
   }



In case you use data source control (part of ASP.NET 2.0), you do not need to subscribe to the NeedDataSource event of the grid but to the PreRender event of the control to perform the initial filtering:

VB.NET Copy Code
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
        If (Not Page.IsPostBack) Then
            RadGrid1.MasterTableView.FilterExpression = "([Country] LIKE '%Germany%') "
            Dim column As GridColumn = RadGrid1.MasterTableView.GetColumnSafe("Country")
            column.CurrentFilterFunction = GridKnownFunction.Contains
            column.CurrentFilterValue = "Germany"
            RadGrid1.MasterTableView.Rebind()
        End If
End Sub

 

C# Copy Code
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
       
if (!Page.IsPostBack)
       {
           RadGrid1.MasterTableView.FilterExpression =
"([Country] LIKE \'%Germany%\') ";

           GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(
"Country");
           column.CurrentFilterFunction = GridKnownFunction.Contains;
           column.CurrentFilterValue =
"Germany";

           RadGrid1.MasterTableView.Rebind();
       }
}



Finally, when you have GridCheckBoxColumn the syntax for the FilterExpression is slightly different, for example:

VB.NET Copy Code
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
        If (Not Page.IsPostBack) Then
            RadGrid1.MasterTableView.FilterExpression = "([chkBoxColumnDataField] = True) "
            Dim column As GridColumn = RadGrid1.MasterTableView.GetColumnSafe("MyCheckBoxColumnUniqueName")
            column.CurrentFilterFunction = GridKnownFunction.EqualTo
            column.CurrentFilterValue = "True"
            RadGrid1.MasterTableView.Rebind()
        End If
End Sub

 

C# Copy Code
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
       
if (!Page.IsPostBack)
       {
           RadGrid1.MasterTableView.FilterExpression =
"([chkBoxColumnDataField] = True) ";

           GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(
"MyCheckBoxColumnUniqueName");
           column.CurrentFilterFunction = GridKnownFunction.EqualTo;
           column.CurrentFilterValue =
"True";

           RadGrid1.MasterTableView.Rebind();
       }
}.