joeharrison714 / MVCGrid.Net

http://mvcgrid.net
MIT License
74 stars 55 forks source link

Sorting is not working #34

Closed Pr-Praveen closed 8 years ago

Pr-Praveen commented 8 years ago

I'm trying to enable sorting in the MVCGrid but is not working

joeharrison714 commented 8 years ago

Hi, Did you enable sorting both on the grid and on the individual columns? If so, are you getting any errors in the browser console? Thanks, Joe

joeharrison714 commented 8 years ago

Be sure to use WithSorting on both the grid and the columns

RafaelLiendo2409 commented 5 years ago

I am trying to use this, but is not working the sorting. This is my code, I enabled sorting in both the grid and columns. Is there an error?. I do not see any error in the console browser. It just do not sort.

Code:

ColumnDefaults colDefaults = new ColumnDefaults() { EnableSorting = true };

MVCGridDefinitionTable.Add("SitiosL", new MVCGridBuilder<Sitios>(colDefaults)
    .WithAuthorizationType(AuthorizationType.AllowAnonymous)
    .AddColumns(cols =>
    {
        // Add your columns here
        cols.Add("ID").WithSorting(false)
            .WithValueExpression(i => i.ID.ToString()); // use the Value Expression to return the cell text for this column
        cols.Add("Sitio").WithSorting(true)
            .WithHeaderText("Sitio")
            .WithValueExpression(i => i.Sitio);
        cols.Add("TipoSitio").WithSorting(false)
        .WithHeaderText("Tipo de Sitio")
        .WithValueExpression(i => i.TipoSitio1.TipoSitio1);

    })
    .WithSorting(true,"Sitio")
    .WithRetrieveDataMethod((context) =>
    {
        var options = context.QueryOptions;
        var result = new QueryResult<Sitios>();
        using (var db = new SIGEMAEntities())
        {
            var query = db.Sitios.Include(s => s.TipoSitio1).ToList();

            if (!String.IsNullOrWhiteSpace(options.SortColumnName))
            {
                switch (options.SortColumnName.ToLower())
                {
                    case "Sitio":

                        if (options.SortDirection == SortDirection.Asc)
                            query = query.OrderBy(c=>c.Sitio).ToList();
                        else query = query.OrderByDescending(c => c.Sitio).ToList();
                        break;

                }
            }

            result.Items = query;
        }

        return result;

    })
);
Anupamabeena commented 5 years ago

Hi you could try changing the case "Sitio" to lowercase "sitio"