Filtering and sorting for GridButtonColumn

Thread is closed for posting
3 posts, 0 answers
  1. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 05 Nov 2007 Link to this post

    Requirements

    RadGrid for ASP .NET version

    RadControls for ASP .NET AJAX version

    5.x and later


    2008.1.415 and later

    .NET version

    2.0 and later

    Visual Studio version

    2005 and later

    Programming language

    VB.NET

    Browser support

    all supported by RadGrid for ASP .NET


    all browsers supported by RadControls for ASP .NET AJAX



    PROJECT DESCRIPTION

    The example illustrates how to extend the built-in GridButtonColumn to support filtering and sorting. The idea is to include SortExpression property for sorting and DataField for filtering along with textbox filter added to the column header. The logic resides in the GridButtonColumnWithFiltering class inside the App_Code folder:

     
    Namespace GridButtonColumnWithFilteringSortingNS  
        Public Class GridButtonColumnWithFilteringSorting  
            Inherits GridButtonColumn  
     
            Public Property DataField() As String 
                Get 
                    Dim MyRes As Object = Me.ViewState("_df")  
                    If (Not MyRes Is NothingThen 
                        Return CType(MyRes, String)  
                    End If 
                    Return String.Empty  
                End Get 
                Set(ByVal Value As String)  
                    Me.ViewState("_df") = Value  
                End Set 
            End Property 
            Public Overrides Property SortExpression() As String 
                Get 
                    Dim MyRes As Object = Me.ViewState("_se")  
                    If (Not MyRes Is NothingThen 
                        Return CType(MyRes, String)  
                    End If 
                    Return String.Empty  
                End Get 
                Set(ByVal Value As String)  
                    Me.ViewState("_se") = 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 
        End Class 
    End Namespace 

    namespace GridButtonColumnWithFilteringSortingNS  
    {  
        public class GridButtonColumnWithFilteringSorting : GridButtonColumn  
        {  
     
            public string DataField   
                    {  
                get   
                            {  
                    object MyRes = this.ViewState["_df"];  
                    if (MyRes != null)  
                    {  
                        return (string)MyRes;  
                    }  
                    return string.Empty;  
                }  
                set { this.ViewState["_df"] = value; }  
            }  
            public override string SortExpression   
                    {  
                get   
                            {  
                    object MyRes = this.ViewState["_se"];  
                    if (MyRes != null)  
                    {  
                        return (string)MyRes;  
                    }  
                    return string.Empty;  
                }  
                set { this.ViewState["_se"] = value; }  
            }  
     
            protected override void SetupFilterControls(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 (!object.ReferenceEquals(this.CurrentFilterValue, string.Empty))  
                {  
                    ((TextBox)cell.Controls[0]).Text = this.CurrentFilterValue;  
                }  
            }  
     
            protected override string GetCurrentFilterValueFromControl(TableCell cell)  
            {  
                return ((TextBox)cell.Controls[0]).Text;  
            }  
     
            public override bool SupportsFiltering()  
            {  
                return true;  
            }  
        }  

    Also note the register tag on the page in which the grid controls resides:

      

    <%@ Register TagPrefix="custom" Namespace="GridButtonColumnWithFilteringSortingNS" %>              
     
    <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
                <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">  
                    <AjaxSettings> 
                        <telerik:AjaxSetting AjaxControlID="RadGrid1">  
                            <UpdatedControls> 
                                <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                            </UpdatedControls> 
                        </telerik:AjaxSetting> 
                    </AjaxSettings> 
                </telerik:RadAjaxManager> 
                <telerik:RadGrid AutoGenerateColumns="false" ID="RadGrid1" DataSourceID="AccessDataSource1" 
                    Skin="WebBlue" AllowFilteringByColumn="True" AllowSorting="True" 
                    PageSize="15" ShowStatusBar="true" AllowPaging="True" runat="server" GridLines="None" 
                    Width="550px">  
                    <PagerStyle Mode="NextPrevAndNumeric" /> 
                    <MasterTableView AutoGenerateColumns="false" EditMode="InPlace" AllowFilteringByColumn="True" 
                        Width="99%" TableLayout="Fixed">  
                        <Columns> 
                            <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" 
                                UniqueName="CustomerID">  
                            </telerik:GridBoundColumn> 
                            <custom:GridButtonColumnWithFilteringSorting HeaderText="ContactName" UniqueName="ContactName" 
                                SortExpression="ContactName" DataTextField="ContactName" DataField="ContactName" 
                                CommandName="MyCommand" ButtonType="PushButton" /> 
                            <telerik:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City" 
                                UniqueName="City" /> 
                        </Columns> 
                    </MasterTableView> 
                </telerik:RadGrid> 
                <br /> 
                <asp:AccessDataSource ID="AccessDataSource1" DataFile="~/App_Data/Nwind.mdb" SelectCommand="SELECT * FROM Customers" 
                    runat="server"></asp:AccessDataSource> 



    <%@ Register TagPrefix="custom" Namespace="GridButtonColumnWithFilteringSortingNS"  %>              
     
     
    <radG:RadGrid AutoGenerateColumns="false" ID="RadGrid1" DataSourceID="AccessDataSource1" 
                    EnableAJAX="True" Skin="Business" AllowFilteringByColumn="True" 
                    AllowSorting="True" PageSize="15" ShowStatusBar="true" AllowPaging="True" runat="server" 
                    GridLines="None" Width="550px">  
                    <PagerStyle Mode="NextPrevAndNumeric" /> 
                    <MasterTableView AutoGenerateColumns="false" EditMode="InPlace" AllowFilteringByColumn="True" 
                     Width="99%" TableLayout="Fixed">  
                        <Columns> 
                            <radG:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" 
                                UniqueName="CustomerID">  
                            </radG:GridBoundColumn> 
                            <custom:GridButtonColumnWithFilteringSorting HeaderText="ContactName" UniqueName="ContactName" SortExpression="ContactName"   
                             DataTextField="ContactName" DataField="ContactName" CommandName="MyCommand" ButtonType="PushButton" /> 
                            <radG:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City" UniqueName="City" /> 
                        </Columns> 
                    </MasterTableView> 
                </radG:RadGrid> 
                <br /> 
                <asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb" 
                    SelectCommand="SELECT * FROM Customers" runat="server"></asp:AccessDataSource> 
  2. Cliff Akers
    Cliff Akers avatar
    10 posts
    Member since:
    Apr 2010

    Posted 12 Jan 2011 Link to this post

    I used the solution posted to implement filtering and sorting for a GridButtonColumn. I have one issue, the FilterControlWidth property is not being applied to the page when filtering is enabled. The text box that is added when filtering is turned on is always the same size as the column header. Any suggestions?

     

    <cc1:GridButtonColumnWithFilteringSorting DataTextField="Form" DataField="Form" SortExpression="Form" HeaderText="Form Number" HeaderButtonType="TextButton" CommandName="OpenCommand" UniqueName="IdLink" DataType="System.String" FilterControlWidth="50px">
    <headerstyle width="80px" />
    </cc1:GridButtonColumnWithFilteringSorting>

     

  3. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 13 Jan 2011 Link to this post

    Hi Cliff,

    FilterControlWidth is not implemented for GridButtonColumn as it does not support built-in filtering. If you want to take advantage of such property, you should either implement it for the extended button column from this code library or adjust the width of the filter textbox manually as explained here.

    Best regards,
    Sebastian
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top