WeyCell
WeyCell

Reputation: 283

MVC4 Devexpress Sorting, Grouping and Filtering not working

I am working on devexpress and having some problems. Here is my code:

settings.Columns.Add(column =>
{
    column.Caption = "Code";
    column.Settings.AllowGroup = DefaultBoolean.True;
    column.SetDataItemTemplateContent(c =>
    {
        ViewContext.Writer.Write(DataBinder.Eval(c.DataItem, "Code"));
    });

    column.SetEditItemTemplateContent(c =>
    {
        if (DataBinder.Eval(c.DataItem, "Code") != null)
        {
            ViewContext.Writer.Write(DataBinder.Eval(c.DataItem, "Code"));
        }
        else
        {
            Html.DevExpress().TextBox(textBox =>
            {
                textBox.Width = Unit.Percentage(100);
                textBox.Name = "Code";
            }).Render();
        }
    });
});

im adding a column like this and it is showing right values, bu sorting or grouping or filtering is not working. How can i make those functions work? Please Help!!

Upvotes: 0

Views: 2084

Answers (1)

Guillermo Gutiérrez
Guillermo Gutiérrez

Reputation: 17799

You are missing the FieldName of the column, which is the name of the column or property in the DataSource to which the Grid is bound:

column.FieldName = "Code";

That allows the control to know which field will filter and sort. Also, you may need to allow sorting and filtering:

column.Settings.AllowSort = DefaultBoolean.True;  // I think this is the default
column.Settings.AllowHeaderFilter = DefaultBoolean.True;

If you need to define other filtering options, you can also set the HeaderFilterFillItems method at the grid settings, and modify the Values collection at the event args parameter:

gridSettings.HeaderFilterFillItems = (sender, e) =>
{
    if (e.Column.FieldName.Equals("Code")) {
        e.Values.Clear();
        e.AddValue("DisplayOption", "Value", "Query");
        // ...
    }
};

UPDATE: If your column has custom data, then it is unbound, and you can use the event CustomColumnUnboundData to define the value of the column, which will also be used to filter/sort the grid:

// Same column definition as yours
settings.Columns.Add(column =>
{
    column.Caption = "Code";
    column.Settings.AllowGroup = DefaultBoolean.True;
    column.SetDataItemTemplateContent(c =>
    {
    ViewContext.Writer.Write(DataBinder.Eval(c.DataItem, "Code"));
    });

    column.SetEditItemTemplateContent(c =>
    {
        if (DataBinder.Eval(c.DataItem, "Code") != null)
        {
            ViewContext.Writer.Write(DataBinder.Eval(c.DataItem, "Code"));
        }
        else
        {
            Html.DevExpress().TextBox(textBox =>
            {
                textBox.Width = Unit.Percentage(100);
                textBox.Name = "Code";
            }).Render();
        }
    });
});

// CustomUnboundColumnData event handler
settings.CustomUnboundColumnData = (sender, e) => {
    if(e.Column.Caption.Equals("Code")) {
        // You can get the value of any existing field in the datasource, this way:
        string code= (string)e.GetListSourceFieldValue("Code");
        // Do some processs to get the custom value
        // ...
        // And set it to the Value propery of the event args parameter
        e.Value = myCustomValue;
    }
};

You can find an example of unboud data in this link: https://www.devexpress.com/Support/Center/Example/Details/E2824

Upvotes: 1

Related Questions