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 first problem is that I am unable to display Engine properties in the gridview using custom databinding.
In the method linked to the gridview's settings.CallbackRouteValues property, I use the GridViewExtension.GetViewModel method to get the gridview's viewmodel. When it is null, I create a new viewmodel and add the same columns that my gridview contains. Then I bind the viewmodel to my data using the ProcessCustomBinding method. My data is a List<Car>.
It works well for Car simple attributes (id, model, brand). However I am not able to display Engine properties.
When I use Linq databinding to display the engine's horsePower, all I have to do is to set the gridview's column fieldname to "Engine.horsePower" This method doesn't seem to work with this binding method. Do I have to manually feed each of the viewmodel field to achieve this?
My second problem is when I try to go to the other pages of the gridview. When my controller paging action is called, the StartDataRowIndex of the GridViewPagerState is always 0. Do you have any clue on what could cause this behavior?
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()
Hello Kévin,
Thank you for contacting us. Please give us some time to check these issues on our side. Your patience is greatly appreciated.