1

I have a DevExpress gridview with a description column that has column header filters enabled. The problem is that having these filters enabled makes it so an entry is added to the drop down for every data item in the list which isn't really desirable because the entries are generally paragraphs of text. Instead I would like to restrict the options to just All, Blanks, and Non blanks but I haven't been able to find a clear example of how this might be accomplished. Thanks for the help!

enter image description here

ANSWER:

settings.HeaderFilterFillItems = (sender, e) =>
{
    if (e.Column.FieldName == "Description")
    {
       e.Values.Clear();
       e.AddShowAll();
       e.Values.Insert(1, FilterValue.CreateShowBlanksValue(e.Column, "(Blanks)"));
       e.Values.Insert(2, FilterValue.CreateShowNonBlanksValue(e.Column, "(Non Blanks)"));
    }
};
Nathan Kamenar
  • 824
  • 7
  • 30
  • I answered it, hope it helps, on the other hand, I use DevExpress in my projects, but for MVC I prefer to use DevExtreme, simpler controls, excelent performance, good sample code and support – Victor Hugo Terceros Sep 07 '17 at 20:36

1 Answers1

1

This question is similar than yours https://www.devexpress.com/Support/Center/Question/Details/Q477323/gridview-how-to-customize-header-filter-items

And here in the view you have the custom filter items, using settings.HeaderFilterFillItems https://demos.devexpress.com/MVCxGridViewDemos/Filtering/Filtering

@Html.DevExpress().GridView(
    settings => {
        settings.Name = "gvFiltering";
        settings.CallbackRouteValues = new { Controller = "Filtering", Action = "FilteringPartial", EnableCheckedListMode = ViewBag.EnableCheckedListMode };
        settings.Width = Unit.Percentage(100);

        settings.Columns.Add("CompanyName");
        settings.Columns.Add("Country");
        settings.Columns.Add("City");
        settings.Columns.Add("UnitPrice").PropertiesEdit.DisplayFormatString = "c";
        settings.Columns.Add("Quantity");
        settings.Columns.Add("Discount").PropertiesEdit.DisplayFormatString = "p0";
        settings.Columns.Add(column => {
            column.FieldName = "Total";
            column.PropertiesEdit.DisplayFormatString = "c";
            column.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            column.UnboundExpression = "UnitPrice * Quantity * (1 - Discount)";
        });

        settings.Settings.ShowHeaderFilterButton = true;
        settings.SettingsPopup.HeaderFilter.Height = 200;

        var headerFilterMode = ViewBag.EnableCheckedListMode ? GridHeaderFilterMode.CheckedList : GridHeaderFilterMode.List;
        foreach(GridViewDataColumn column in settings.Columns)
            column.SettingsHeaderFilter.Mode = headerFilterMode;

        settings.HeaderFilterFillItems = (sender, e) => {
            ASPxGridView grid = (ASPxGridView)sender;
            if(e.Column.FieldName == "Total") {
                e.Values.Clear();
                if(e.Column.SettingsHeaderFilter.Mode == GridHeaderFilterMode.List)
                e.AddShowAll();
                int step = 100;
                for(int i = 0; i < 10; i++) {
                    double start = step * i;
                    double end = start + step - 0.01;
                    e.AddValue(string.Format("from {0:c} to {1:c}", start, end), string.Empty, string.Format("[Total] >= {0} and [Total] <= {1}", start, end));
                }
                e.AddValue(string.Format("> {0:c}", 1000), string.Empty, "[Total] > 1000");
            } else if(e.Column.FieldName == "Quantity") {
                int max = 0;
                for(int i = 0; i < e.Values.Count; i++) {
                    int value;
                    if(!int.TryParse(e.Values[i].Value, out value))
                        continue;
                    if(value > max)
                        max = value;
                }
                e.Values.Clear();
                if(e.Column.SettingsHeaderFilter.Mode == GridHeaderFilterMode.List)
                e.AddShowAll();
                int step = 10;
                for(int i = 0; i < max / step + 1; i++) {
                    int start = step * i;
                    int end = start + step - 1;
                    e.AddValue(string.Format("from {0} to {1}", start, end), string.Empty, string.Format("[Quantity] >= {0} and [Quantity] <= {1}", start, end));
                }
            }
        };
    }).Bind(Model).GetHtml()
Victor Hugo Terceros
  • 2,969
  • 3
  • 18
  • 31
  • This didn't directly answer my question but it was enough info to point me to where I needed to get it working. I have updated my question with the actual implementation I used to accomplish what I was intending. Thanks! – Nathan Kamenar Sep 08 '17 at 13:28