AutoFilterMode Property

WorksheetBase.AutoFilterMode Property

 

Gets or sets a value that indicates whether filtering is currently enabled on the worksheet (that is, whether the filter drop-down arrows are currently displayed).

Namespace:   Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v4.0.Utilities (in Microsoft.Office.Tools.Excel.v4.0.Utilities.dll)

public bool AutoFilterMode { get; set; }

Property Value

Type: System.Boolean

true if filtering is currently enabled on the worksheet; otherwise, false.

Exception Condition
COMException

This property is set to true.

You can set this property to false to disable filtering (that is, to remove the filter drop-down arrows), but you cannot set it to true. To enable filtering and create a filtered list, use the M:Microsoft.Office.Interop.Excel.Range.AutoFilter(System.Object,System.Object,Microsoft.Office.Interop.Excel.XlAutoFilterOperator,System.Object,System.Object) method of a Range object, or use the AutoFilter method of a NamedRange object.

This property indicates only whether filtering is enabled; it does not indicate whether any data is being filtered. To determine whether the worksheet is actively filtering data, use the FilterMode property.

The following code example creates a filtered list of fruits in a Range, and then uses the AutoFilter property to display the filter criteria for the list. The example then prompts the user to turn off the filter by using the AutoFilterMode property, and finally uses the FilterMode property to display whether the worksheet has a filtered list.

This example is for a document-level customization.

private void ActivateAutoFilter()
{
    this.Range["A1"].Value2 = "Fruits";
    this.Range["A2"].Value2 = "Banana";
    this.Range["A3"].Value2 = "Apple";
    this.Range["A4"].Value2 = "Banana";
    this.Range["A5"].Value2 = "Orange";
    this.Range["A6"].Value2 = "Apple";
    this.Range["A7"].Value2 = "Orange";

    Microsoft.Office.Tools.Excel.NamedRange NamedRange1 =
        this.Controls.AddNamedRange(this.Range["A1", "A7"],
        "NamedRange1");

    NamedRange1.AutoFilter(1, "Apple",
       Excel.XlAutoFilterOperator.xlAnd, true);

    MessageBox.Show("The current criteria for the AutoFilter is: " +
        this.AutoFilter.Filters[1].Criteria1.ToString());

    if (this.AutoFilterMode)
    {
        if (DialogResult.Yes == MessageBox.Show("Would you like to " +
            "turn off the filter?", "Example", MessageBoxButtons.YesNo))
        {
            this.AutoFilterMode = false;
        }
    }

    if (this.FilterMode)
    {
        MessageBox.Show("The worksheet has a filtered list.");
    }
    else
    {
        MessageBox.Show("The worksheet does not have a filtered list");
    }
}
Return to top
Show:
© 2016 Microsoft