Skip to main content
A newer version of this page is available. .
Tab

ASPxGridView.AutoFilterCellEditorCreate Event

Enables you to assign editors to individual filter row cells.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v20.2.dll

NuGet Package: DevExpress.Web

Declaration

public event ASPxGridViewEditorCreateEventHandler AutoFilterCellEditorCreate

Event Data

The AutoFilterCellEditorCreate event's data class is ASPxGridViewEditorCreateEventArgs. The following properties provide information specific to this event:

Property Description
Column Gets the data column to which the processed filter row cell corresponds.
EditorProperties Gets or sets the filter cell’s editor. Inherited from ASPxGridEditorCreateEventArgs.
KeyValue Gets the row key value - an object that uniquely identifies the row.
Value Gets or sets the filter cell’s value. Inherited from ASPxGridEditorCreateEventArgs.
VisibleIndex Gets the row’s visible index.

Remarks

The AutoFilterCellEditorCreate event is raised for each cell within the filter row, and enables you to replace their default editors with custom ones.

The column to which the processed cell corresponds is returned by the ASPxGridViewEditorCreateEventArgs.Column property.

To change the cell’s editor, use the event parameter’s ASPxGridEditorCreateEventArgs.EditorProperties property. The cell’s value is specified by the ASPxGridEditorCreateEventArgs.Value property.

To learn more, see Filtering.

Example

This example demonstrates how to create an ASPxComboBox filter with “today”, “yesterday” and other filtering options.

View Example

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DevExpress.Web;
using DevExpress.Web.Data;
using DevExpress.Data.Filtering;
using System.Globalization;

public partial class _Default : System.Web.UI.Page {
    protected void Page_Init(object sender, EventArgs e) {
        DataTable table = new DataTable();
        DataColumn column = table.Columns.Add("Id", typeof(Int32));

        table.PrimaryKey = new DataColumn[] { column };
        table.Columns.Add("Text", typeof(String));
        table.Columns.Add("Date", typeof(DateTime));

        DateTime today = DateTime.Now;

        if (hf.Contains("today"))
            today = Convert.ToDateTime(hf.Get("today"));
        else
            hf.Set("today", today);

        table.Rows.Add(new Object[] { 0, "Text - today", today });
        table.Rows.Add(new Object[] { 1, "Text - yesterday", today.Subtract(new TimeSpan(1, 0, 0, 0)) });

        for (int i = 0; i < 40; i++)
            table.Rows.Add(new Object[] { i + 2, String.Format("Text{0}", i), today.Subtract(new TimeSpan(i * 10, i, 0, 0)) });

        grid.DataSource = table;
        grid.DataBind();
    }

    protected void grid_AutoFilterCellEditorCreate(object sender, ASPxGridViewEditorCreateEventArgs e) {
        if (e.Column.FieldName == "Date") {
            ComboBoxProperties combo = new ComboBoxProperties();
            e.EditorProperties = combo;
        }
    }

    protected void grid_AutoFilterCellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {
        if (e.Column.FieldName == "Date") {
            ASPxComboBox combo = e.Editor as ASPxComboBox;
            combo.ValueType = typeof(string);

            combo.Items.Add("Today");
            combo.Items.Add("Yesterday");
            combo.Items.Add("This week");
            combo.Items.Add("Last week");
            combo.Items.Add("This month");
            combo.Items.Add("Last month");
            combo.Items.Add("This year");
            combo.Items.Add("Last year");

        }
    }

    protected void grid_ProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e) {
        if (e.Column.FieldName != "Date") return;
        if (e.Kind == GridViewAutoFilterEventKind.ExtractDisplayText && Session["value"] != null) {
            e.Value = Session["value"].ToString();
            return;
        }
        else {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day),
                end = start;
            string value = e.Value;

            if (value == "Today")
                end = end.AddDays(1.0).AddMilliseconds(-1);
            else if (value == "Yesterday") {
                start = start.AddDays(-1.0);
                end = end.AddMilliseconds(-1.0);
            }
            else if (value == "This week") {
                start = start.Subtract(new TimeSpan(7, 0, 0, 0));
                end = end.AddDays(1.0).AddMilliseconds(-1);
            }
            else if (value == "Last week") {
                start = start.Subtract(new TimeSpan(14, 0, 0, 0));
                end = end.Subtract(new TimeSpan(7, 0, 0, 0, 1));
            }
            else if (value == "This month") {
                start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                end = start.AddMonths(1).AddMilliseconds(-1.0);
            }
            else if (value == "Last month") {
                start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1);
                end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMilliseconds(-1.0);
            }
            else if (value == "This year") {
                start = new DateTime(DateTime.Now.Year, 1, 1);
                end = start.AddYears(1).AddMilliseconds(-1.0);
            }
            else if (value == "Last year") {
                start = new DateTime(DateTime.Now.Year - 1, 1, 1);
                end = new DateTime(DateTime.Now.Year, 1, 1).AddMilliseconds(-1);
            }
            Session["value"] = value;
            e.Criteria = new GroupOperator(GroupOperatorType.And,
                new BinaryOperator(e.Column.FieldName, start, BinaryOperatorType.GreaterOrEqual),
                new BinaryOperator(e.Column.FieldName, end, BinaryOperatorType.Less));

        }
    }
}
See Also