Ticket Q370070
Visible to All Users

GridView - How to set PageSize in a custom binding mode

created 12 years ago (modified 12 years ago)

[DX Support team: this thread was cloned from GridView - Сustom databinding issues]
Hello,

I'm trying the new custom binding mode on gridviews and I have several problems implementing it.
I try do display several objects which contain other objects. Here is a simplified example of what my objects looks like :

C#
public class Car { public int id; public Engine engine; public string model; public string brand; } public class Engine { public int id; public string brand; public string model; public int horsePower; }

My last problem is that settings.SettingsPager.PageSize that I define in my gridview seems to be ignored. When I get my viewmodel in my controller, it's Pager.PageSize property is always 10 unless I set it to the desired value when I create my viewmodel on the first call of the CallbackRouteValues method. Is there a way to force the use of the value defined in the gridview? Thanks. Here is my controller code :

C#
public class CarController : Controller { [HttpGet, ActionName(CarControllerAction.Car)] public ActionResult Car() { return View(CarControllerAction.Car); } [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post), ActionName(CarControllerAction.Car_Partial)] public PartialViewResult Car_Partial() { var viewModel = GridViewExtension.GetViewModel("gvCar"); if (viewModel == null) { viewModel = new GridViewModel(); viewModel.KeyFieldName = "ID"; viewModel.Columns.Add("ID"); viewModel.Columns.Add("Model"); viewModel.Columns.Add("Brand"); viewModel.Columns.Add("Engine.ID"); viewModel.Columns.Add("Engine.Brand"); viewModel.Columns.Add("Engine.Model"); viewModel.Columns.Add("Engine.HorsePower"); } viewModel.ProcessCustomBinding( MyBindingHandlers.MyGetDataRowCount, MyBindingHandlers.MyGetData); return PartialView(CarControllerAction.Car_Partial, viewModel); } [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post), ActionName(CarControllerAction.Car_Partial_Paging)] public PartialViewResult Car_Partial_Paging(GridViewPagerState pager) { var viewModel = GridViewExtension.GetViewModel("gvCar"); viewModel.Pager.Assign(pager); // pager.PageIndex is always 0 ???!!! viewModel.ProcessCustomBinding( MyBindingHandlers.MyGetDataRowCount, MyBindingHandlers.MyGetData); return PartialView(CarControllerAction.Car_Partial, viewModel); } } public class CarControllerAction { public const string Car = "Car"; public const string Car_Partial = "Car_Partial"; public const string Car_Partial_Paging = "Car_Partial_Paging"; } public static class MyBindingHandlers { static IQueryable<Car> Model { get { return new CarDAO<Car>().GetAll_Query(); } } public static void MyGetDataRowCount(GridViewCustomBindingGetDataRowCountArgs e) { e.DataRowCount = Model.Count(); } public static void MyGetData(GridViewCustomBindingGetDataArgs e) { e.Data = Model .Skip(e.StartDataRowIndex) .Take(e.DataRowCount) .ToList(); } }

And here is my view :

C#
@model GridViewModel @Html.DevExpress().GridView( settings => { settings.Name = "gvCar"; settings.CallbackRouteValues = new { Controller = "CarController", Action = CarControllerAction.Car_Partial }; settings.CustomBindingRouteValuesCollection.Add( GridViewOperationType.Paging, new { Controller = "CarController", Action = CarControllerAction.Car_Partial_Paging } ); settings.KeyFieldName = "ID"; settings.Width = System.Web.UI.WebControls.Unit.Percentage(100); settings.Settings.ShowColumnHeaders = true; settings.Settings.ShowFilterRow = true; settings.Settings.ShowFilterRowMenu = true; settings.Settings.ShowFooter = true; settings.Settings.ShowGroupButtons = true; settings.Settings.ShowGroupedColumns = true; settings.Settings.ShowGroupPanel = true; settings.Settings.ShowHeaderFilterButton = true; settings.Settings.ShowTitlePanel = true; settings.Settings.UseFixedTableLayout = true; settings.SettingsBehavior.AllowDragDrop = true; settings.SettingsBehavior.AllowFocusedRow = true; settings.SettingsBehavior.AllowGroup = true; settings.SettingsBehavior.AllowSort = true; settings.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.NextColumn; settings.SettingsBehavior.ConfirmDelete = true; settings.SettingsBehavior.EnableRowHotTrack = true; settings.SettingsPager.FirstPageButton.Visible = true; settings.SettingsPager.LastPageButton.Visible = true; settings.SettingsPager.PageSize = 50; settings.SettingsPager.PageSizeItemSettings.Visible = true; settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" }; settings.SettingsCookies.Enabled = true; settings.SettingsCookies.CookiesID = settings.Name; settings.SettingsCookies.StoreColumnsVisiblePosition = true; settings.SettingsCookies.StoreColumnsWidth = true; settings.SettingsCookies.StoreFiltering = true; settings.SettingsCookies.StoreGroupingAndSorting = true; settings.SettingsCookies.StorePaging = true; settings.SettingsBehavior.EnableCustomizationWindow = true; settings.SettingsPopup.CustomizationWindow.Height = 500; settings.SettingsPopup.CustomizationWindow.Width = 250; settings.Columns.Add(column => { column.FieldName = "ID"; column.ColumnType = MVCxGridViewColumnType.TextBox;}); settings.Columns.Add(column => { column.FieldName = "Model"; column.ColumnType = MVCxGridViewColumnType.TextBox;}); settings.Columns.Add(column => { column.FieldName = "Brand"; column.ColumnType = MVCxGridViewColumnType.TextBox;}); settings.Columns.Add(column => { column.FieldName = "Engine.ID"; column.ColumnType = MVCxGridViewColumnType.TextBox;}); settings.Columns.Add(column => { column.FieldName = "Engine.Brand"; column.ColumnType = MVCxGridViewColumnType.TextBox;}); settings.Columns.Add(column => { column.FieldName = "Engine.Model"; column.ColumnType = MVCxGridViewColumnType.TextBox;}); settings.Columns.Add(column => { column.FieldName = "Engine.HorsePower"; column.ColumnType = MVCxGridViewColumnType.TextBox;}); } ).BindToCustomData(Model).GetHtml()

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hello Kévin,
ASPxGridViewPagerSettings.PageSize is a property that shows how many data rows we can get from a data set. If you use Custom Binding mode, you get all required data in the Model. Therefore, GridViewModel should know about an actual value of the PageSize setting.
To resolve the issue, set the GridViewPagerState.PageSize property in the GridViewModel object.

    Show previous comments (8)
    DevExpress Support Team 12 years ago

      @CORPTAX Development,
      You can also use the GridViewSettings.SettingsPager.PageSize property directly:

      C#
      settings.SettingsPager.PageSize = 24;

      or

      C#
      settings.SettingsPager.PageSize = (int)ViewData["PageSize"];
      CD CD
      CORPTAX Development 12 years ago

        I tried that as well, and it didn't work, and that was the whole issue. Did you have a chance to try it yourself?

        DevExpress Support Team 12 years ago

          @CORPTAX Development,
          Please accept my sincere apologies for the mismatch. If you wish to set the PageIndex initially, you can do this in the PreRender and BeforeGetCallbackResult events (GridView - PageSize in a Pager Template via Custom Callback). In addition, you can set the PageIndex for the GridView model:

          C#
          viewModel.Pager.PageSize = 5;

          I have attached a sample project to this Comment and Answer to show this solution in action. Please check it.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.