Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Grid with DataTable works for MVC but not for Core #8095

Open
eyupyusein opened this issue Jan 9, 2025 · 0 comments
Open

Dynamic Grid with DataTable works for MVC but not for Core #8095

eyupyusein opened this issue Jan 9, 2025 · 0 comments

Comments

@eyupyusein
Copy link
Contributor

The following configuration works in UI for MVC but not in UI for Core:

View:

@(Html.Kendo().Grid<dynamic>()
          .Name("grid")
          .Columns(columns =>
          {
              if (Model != null)
              {
                  foreach (System.Data.DataColumn column in Model.Columns)
                  {
                      if (column.DataType == typeof(DateTime) ||
                        column.ColumnName.Contains("Date"))
                      {
                          columns.Bound(column.ColumnName).Format("{0:MM/dd/yyyy}");
                      }
                      else
                      {
                          columns.Bound(column.ColumnName);
                      }
                  }
              }
          })
          .DataSource(dataSource => dataSource
           .Ajax()
           .Model(model =>
           {
               if (Model != null)
               {
                   foreach (System.Data.DataColumn column in Model.Columns)
                   {
                       model.Field(column.ColumnName, column.DataType);
                   }
               }
           })
           .Read(read => read.Action("ReadData", "Home"))
         )
         .Scrollable()
    )

Controller:

        public IActionResult Index()
        {
            return View(GetGridDataSource());
        }

        public ActionResult ReadData([DataSourceRequest] DataSourceRequest request)
        {
            DataTable dataTable = GetGridDataSource();

            for (int i = 0; i <= 80; i++)
            {
                DataRow row = dataTable.NewRow();
                row["OrderID"] = i + 1;
                if (i % 3 == 0)
                {
                    row["OrderDate"] = DBNull.Value;
                }
                else
                {
                    row["OrderDate"] = DateTime.Now.AddDays(i);
                }
                row["Freight"] = (i + 1) + (i + 1) * 0.1 + (i + 1) * 0.01;
                row["ShipName"] = "Name " + (i + 1);
                row["ShipCountry"] = i <= 5 ? null : "Country " + (i + 1);
                dataTable.Rows.Add(row);
            }

            return Json(dataTable.ToDataSourceResult(request));
        }

        private DataTable GetGridDataSource()
        {
            DataTable dataTable = new DataTable();

            DataColumn column = new DataColumn();
            column.DataType = Type.GetType("System.Int32");
            column.ColumnName = "OrderID";
            dataTable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.DateTime");
            column.ColumnName = "OrderDate";
            dataTable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.Decimal");
            column.ColumnName = "Freight";
            dataTable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "ShipName";
            dataTable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "ShipCountry";
            dataTable.Columns.Add(column);

            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = dataTable.Columns["OrderID"];
            dataTable.PrimaryKey = PrimaryKeyColumns;

            return dataTable;
        }

In MVC project (expected outcome):

Image

In Core project:

Image

Error:

Image

@github-actions github-actions bot added the FP: Unplanned Sync status with associated Feedback Item label Jan 9, 2025
@eyupyusein eyupyusein added C: Grid S: Wrappers (ASP.NET Core) and removed FP: Unplanned Sync status with associated Feedback Item labels Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant