RadGrid for ASP.NET

Implementing filtering for template columns Send comments on this topic.
Filtering > How to > Implementing filtering for template columns

Glossary Item Box

Since version 4.0.0 of Telerik RadGrid you can enable filtering for template columns automatically - just set AllowFilteringByColumn = true for the entire grid and you should be done. Also make sure that the AllowFiltering property of the template column is set to true (this is the default value). You can see this in reality on this online demo of the product (TemplateColumn City is the target template column).

If you would like to support filtering for GridTemplateColumns, you can extend the default GridTemplateColumn class (part of the Telerik RadGrid object model) and add a textbox control to it. Thus you will be able to filter the GridTemplateColumn values exactly the same way as with other Telerik RadGrid column types. For further details please refer to the implementation below:

ASPX/ASCX Copy Code
<%@ Register TagPrefix="custom" Namespace="<MyApplicationNamespace>" Assembly="<MyApplicationAssembly>" %>

  
<rad:radgrid id="RadGrid1" AllowPaging="True" AutoGenerateColumns="False" AllowFilteringByColumn="True" runat="server">
   
<Columns>
    
<custom:MyCustomFilteringColumn DataField="Country" HeaderText="Country">
     
<ItemTemplate>
        
<%# DataBinder.Eval(Container.DataItem, "Country") %>
     
</ItemTemplate>
    
</custom:MyCustomFilteringColumn>
   
</Columns>
  
</rad:radgrid>

And in the code-behind:

C# Copy Code
public class MyCustomFilteringColumn : GridTemplateColumn
{
 
public string DataField
 {
  get
  {
   
object res = this.ViewState["_df"];
   
if ( res != null )
   {
    
return (string)res;
   }

   
return String.Empty;
  }
  set
  {
   
this.ViewState["_df"] = value;
  }
 }

 
protected override void SetupFilterControls(System.Web.UI.WebControls.TableCell cell)
 {
  
base.SetupFilterControls (cell);
  cell.Controls.RemoveAt(0);
  cell.Controls.AddAt(0,
new TextBox());
 }
 
public override GridColumn Clone()
 {
       
return base.Clone();
 }

 
protected override string GetFilterDataField()
 {
  
return this.DataField;
 }

 
protected override void SetCurrentFilterValueToControl(TableCell cell)
 {
  
base.SetCurrentFilterValueToControl (cell);
  
if (this.CurrentFilterValue != "")
  {
   (cell.Controls[0]
as TextBox).Text = this.CurrentFilterValue;
  }
 }

 
protected override string GetCurrentFilterValueFromControl(TableCell cell)
 {
  
return (cell.Controls[0] as TextBox).Text;
 }

 
public override bool SupportsFiltering()
 {
  
return true;
 }

}
VB.NET Copy Code
Public Class MyCustomFilteringColumn
    Inherits GridTemplateColumn

    Public Property DataField() As String
         Get
             Dim MyRes As Object = Me.ViewState("_df")
             If (Not MyRes Is Nothing) Then
                 Return CType(MyRes, String)
             End If
             Return String.Empty
         End Get
         Set(ByVal Value As String)
             Me.ViewState("_df") = Value
         End Set
    End Property

    Protected Overrides Sub SetupFilterControls(ByVal cell As TableCell)
        MyBase.SetupFilterControls(cell)
        cell.Controls.RemoveAt(0)
        cell.Controls.AddAt(0, New TextBox)
    End Sub

    Public Overrides Function Clone() As GridColumn
        Return MyBase.Clone()
    End Function

    Protected Overrides Function GetFilterDataField() As String
         Return Me.DataField
    End Function

    Protected Overrides Sub SetCurrentFilterValueToControl(ByVal cell As TableCell)
        MyBase.SetCurrentFilterValueToControl(cell)

         If Not Me.CurrentFilterValue Is String.Empty Then
            CType(cell.Controls(0), TextBox).Text = Me.CurrentFilterValue
         End If
    End Sub

    Protected Overrides Function GetCurrentFilterValueFromControl(ByVal cell As TableCell) As String
         Return CType(cell.Controls(0), TextBox).Text
    End Function

    Public Overrides Function SupportsFiltering() As Boolean
         Return True
    End Function