This is a migrated thread and some comments may be shown as answers.

Clear Filters button in Filter Row

2 Answers 118 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 11 Oct 2011, 03:15 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Tim Weckx
Top achievements
Rank 2
answered on 11 Oct 2011, 05:46 PM
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
0
Dave
Top achievements
Rank 1
answered on 15 Oct 2011, 02:40 PM
Thank you for your solution, it works beautifully!
Tags
GridView
Asked by
Dave
Top achievements
Rank 1
Answers by
Tim Weckx
Top achievements
Rank 2
Dave
Top achievements
Rank 1
Share this question
or