Clear Filters button in Filter Row

3 posts, 1 answers
  1. Euroscan
    Euroscan avatar
    26 posts
    Member since:
    Apr 2006

    Posted 11 Oct 2011 Link to this post

    I want to have a button in the filter row for cancelling all active filters.
    Just like the Clear filters button in http://demos.telerik.com/aspnet-ajax/grid/examples/programming/filtertemplate/defaultcs.aspx

    Is this possible in winforms?
  2. Answer
    Tim Weckx
    Tim Weckx avatar
    4 posts
    Member since:
    Jul 2012

    Posted 11 Oct 2011 Link to this post

    Bernd,

    We have done something similar in one of our projects by creating a custom filtercellelement.

    /// <summary>
    /// Represents a filtercell that will clear all filters.
    /// </summary>
    public class ClearAllFiltersCell : GridFilterCellElement
    {
        private RadButtonElement btnClearAll;
     
        /// <summary>
        /// Initializes a new instance of the <see cref="CheckBoxHeaderCell"/> class.
        /// </summary>
        /// <param name="column"></param>
        /// <param name="row"></param>
        public ClearAllFiltersCell(GridViewDataColumn column, GridRowElement row)
            : base(column, row)
        {
        }
     
        /// <summary>
        /// Creates the child elements.
        /// </summary>
        protected override void CreateChildElements()
        {
            // The clear all button
            btnClearAll = new RadButtonElement();
            btnClearAll.Text = "Clear All";
            btnClearAll.Click += new EventHandler(btnClearAll_Click);
             
            // Create the child elements, otherwise the necessary elements are not in place
            base.CreateChildElements();
     
            // Clear all children
            this.Children.Clear();
     
            // Add our button
            this.Children.Add(btnClearAll);
        }
     
        /// <summary>
        /// Adds the editor.
        /// </summary>
        /// <param name="editor">The editor.</param>
        public override void AddEditor(IInputEditor editor)
        {
            // Override this and leave it blank. Clicking on the clear all button will
            // still trigger adding an editor and we dont' want that.
        }
     
        /// <summary>
        /// Arranges the override.
        /// </summary>
        /// <param name="finalSize">The final size.</param>
        /// <returns></returns>
        protected override SizeF ArrangeOverride(SizeF finalSize)
        {
            // This will correctly set the cell size so the clear all button is centered in the cell
            SizeF size = base.ArrangeOverride(finalSize);
     
            RectangleF rect = GetClientRectangle(finalSize);
            this.btnClearAll.Arrange(new RectangleF((finalSize.Width - this.btnClearAll.DesiredSize.Width) / 2, (rect.Height - 20) / 2, 20, 20));
     
            return size;
        }
     
        /// <summary>
        /// Handles the Click event of the btnClearAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void btnClearAll_Click(object sender, EventArgs e)
        {
            // Clear all the filter decriptors
            this.GridControl.FilterDescriptors.Clear();
        }
    }

    Then in your code, hook up to the CreateCell event and add the following:
    void radGridView1_CreateCell(object sender, Telerik.WinControls.UI.GridViewCreateCellEventArgs e)
        {           
            if (e.Row is GridFilterRowElement)
            {
                // Replace "Id" with the column name where you want the Clear All button
                if (e.Column == radGridView1.Columns["Id"])
                {
                    e.CellElement = new ClearAllFiltersCell((GridViewDataColumn)e.Column, e.Row);
                }
            }
        }
    }

    Not sure if it's the best way of doing it, but this works for me

    Tim
  3. Euroscan
    Euroscan avatar
    26 posts
    Member since:
    Apr 2006

    Posted 15 Oct 2011 Link to this post

    Thank you for your solution, it works beautifully!
Back to Top