joeharrison714 / MVCGrid.Net

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

MvcGrid.Net Refresh Data with new values #71

Open siddaram1974 opened 8 years ago

siddaram1974 commented 8 years ago

Hi, i am using this MVCGrid.Net for first time but i am not able to load data dynamically. thanks advance for your help.

arjenbolten commented 8 years ago

I used MVCGridDefinitionTable, However i cant get the paging to work in this case. Could you let me know if you got the paging working?


public void UpdateGrid(List<Model> lstModels)
        {
            GridDefinition<Model> def = MVCGridDefinitionTable.GetDefinition<Model>("GridName");

            def.RetrieveData = (options) =>
            {
                List<Model> lstModels = _context.Model.ToList(); // New values
                var result = new QueryResult<Model>();
                var query = lstModels.AsQueryable();
                result.TotalRecords = query.Count();
                result.Items = query.ToList();

                return result;
            };

Edit: Got paging working now

matagatoz commented 7 years ago

Can you share your code ?

arjenbolten commented 7 years ago
        public void UpdateGrid2()
        {
            try
            {
                // Get definition of the grid you want to update
                GridDefinition<YourModel> def = MVCGridDefinitionTable.GetDefinition<YoutModel>("YourGridName");

                // Set additional query options
                def.Sorting = true;
                def.DefaultSortColumn = "Name";
                def.Paging = true;
                def.ItemsPerPage = 10;
                def.MaxItemsPerPage = 10;

                // Definde gridcontext
                def.RetrieveData = (context) =>
                {

                    var options = context.QueryOptions;

                    // Assign result, needed for paging
                    var result = new QueryResult<YourModel>();

                    // Get Model content list from Application db context
                    List<YourModel> lstOfModel = _context.YourModel.ToList();
                    IQueryable<YouModel> query;

                    query = ListOfModel.AsQueryable();

                    result.TotalRecords = query.Count();

                    // GetLimitRowcount
                    if (options.GetLimitOffset().HasValue)
                    {
                        query = query.Skip(options.GetLimitOffset().Value).Take(options.GetLimitRowcount().Value);
                    }

                    result.Items = query.ToList();

                    return result;
                };
            }
            catch (Exception ex)
            {
                // Handle your Exception
            }
        }