Change a column in Kendo UI Grid

Ctrl_Alt_Defeat

I have an ASP.NET MVC 5 application. Kendo UI grids calls to Web API controller to retrieve the data. I have a requirement to change the contents of a column depending on what Fuel type car is. So on my model for the page I created a string for Fuel Type. In MVC controller this is then set as below:

model.FuelType = "Diesel"; //just hardcoded for testing in reality will be dynamically set by User in a Previous screen - could be set to Petrol in which case I need to change column data

On a partial view I have my Kendo UI Grid defined as below - also on the PartialView I have a html.hiddenfor to store the value of FuelType

  @(Html.Kendo().Grid<Car.Web.Models.CarViewModel>()
                          .Name("CarViewGrid")
                          .Columns(columns =>
                          {
                              columns.Bound(c => c.Make).Width(180);
                              columns.Bound(c => c.ModelNo).Width(280);
                              columns.Bound(c => c.EngineSize).Width(120);
                              columns.Bound(c => c.Colour).Width(65);
                              columns.Bound(c => c.MPG).Width(65);

                              columns.Bound(c => c.CarId)
                                  .Title("Include in Report")
                                  .ClientTemplate("<input type=\"checkbox\" name=\"selectedIds\" value=\"#=CarId#\" class=\"checkboxClass\" />")
                                      .Width(90)
                                      .Sortable(false); 
                          })
                          .Events(builder => builder.DataBound("gridDataBound"))
                          .HtmlAttributes(new { style = "height: 420px;" })
                          .Scrollable()
                          .Sortable(sortable => sortable
                              .AllowUnsort(true)
                              .SortMode(GridSortMode.MultipleColumn))
                          .Pageable(pageable => pageable
                              .PageSizes(true)
                              .ButtonCount(5))
                          .DataSource(dataSource => dataSource
                              .Ajax()
                             .Read(read => read.Url("api/Car/GetCarDetails").Type(HttpVerbs.Get).Data("GetCustId"))
                          )
                          )

I have left a line blank in my column and that is where I need to include a different column - if the FuelTyle is Petrol I need to have

columns.Bound(c => c.NumberSparkPlugs).Width(65);

but if the FuelType is Diesel I need to have

columns.Bound(c => c.Torque).Width(65);

Both of these are string properties on my CarViewModel.

Has anyone done something similar with Kendo UI Grid or can advise in the best approach to achieve this?

CodingWithSpike

I don't have a Kendo MVC project in VisualStudio to test this with, but can't you just use an if statement:

                      .Columns(columns =>
                      {
                          columns.Bound(c => c.Make).Width(180);
                          columns.Bound(c => c.ModelNo).Width(280);
                          columns.Bound(c => c.EngineSize).Width(120);
                          columns.Bound(c => c.Colour).Width(65);
                          columns.Bound(c => c.MPG).Width(65);

                          columns.Bound(c => c.CarId)
                              .Title("Include in Report")
                              .ClientTemplate("<input type=\"checkbox\" name=\"selectedIds\" value=\"#=CarId#\" class=\"checkboxClass\" />")
                                  .Width(90)
                                  .Sortable(false); 

// added code here...
                          if(Model.FuelType == "Petrol")
                              columns.Bound(c => c.NumberSparkPlugs).Width(65);
                          else
                              columns.Bound(c => c.Torque).Width(65);
                      })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Kendo UI grid URL Column

From Dev

Kendo UI grid template column

From Dev

kendo ui grid background change event

From Dev

kendo ui grid background change event

From Dev

Kendo UI Grid: select entire column

From Dev

Kendo UI Grid Checkbox Column Field Not Defined

From Dev

Kendo UI grid with template column and filtering

From Dev

Typescript Kendo UI grid column type error

From Dev

kendo ui grid column template not being applied

From Dev

How to change row color in Kendo UI Grid based on hidden column ID?

From Dev

Kendo UI data grid: How to change column display value based on status flag integer value

From Dev

How to add Kendo WYSIWYG Editor to a Kendo UI Grid Column?

From Dev

How to get column total of Kendo-UI grid column (calculated)

From Dev

How to get column total of Kendo-UI grid column (calculated)

From Dev

How to change the default value of Grid page size in Kendo UI?

From Dev

How to change the default filter operator in kendo ui grid mvc

From Dev

How to change the default filter operator in kendo ui grid mvc

From Dev

how to check and change color of cell value in kendo ui grid

From Dev

Kendo UI Grid from Html table - change event will not fire

From Dev

How to change the background of a cell dynamically in a Kendo MVC UI Grid?

From Dev

How to set Kendo UI grid column widths programmatically

From Dev

Is it possible (and if so how) to add an item to the column menu of a kendo UI grid?

From Dev

Auto incrementing column in Kendo UI Grid with ASP.NET MVC

From Dev

Kendo UI Grid local data source column sort by default

From Dev

How to mask phone number input in Kendo UI Grid Column

From Dev

Using templates&editors in grid column with Angular Kendo UI

From Dev

Use column.values together with row template in Kendo UI Grid

From Dev

escape JavaScript dot in kendo ui grid column field

From Dev

Kendo UI Grid handling missing values in column templates

Related Related

  1. 1

    Kendo UI grid URL Column

  2. 2

    Kendo UI grid template column

  3. 3

    kendo ui grid background change event

  4. 4

    kendo ui grid background change event

  5. 5

    Kendo UI Grid: select entire column

  6. 6

    Kendo UI Grid Checkbox Column Field Not Defined

  7. 7

    Kendo UI grid with template column and filtering

  8. 8

    Typescript Kendo UI grid column type error

  9. 9

    kendo ui grid column template not being applied

  10. 10

    How to change row color in Kendo UI Grid based on hidden column ID?

  11. 11

    Kendo UI data grid: How to change column display value based on status flag integer value

  12. 12

    How to add Kendo WYSIWYG Editor to a Kendo UI Grid Column?

  13. 13

    How to get column total of Kendo-UI grid column (calculated)

  14. 14

    How to get column total of Kendo-UI grid column (calculated)

  15. 15

    How to change the default value of Grid page size in Kendo UI?

  16. 16

    How to change the default filter operator in kendo ui grid mvc

  17. 17

    How to change the default filter operator in kendo ui grid mvc

  18. 18

    how to check and change color of cell value in kendo ui grid

  19. 19

    Kendo UI Grid from Html table - change event will not fire

  20. 20

    How to change the background of a cell dynamically in a Kendo MVC UI Grid?

  21. 21

    How to set Kendo UI grid column widths programmatically

  22. 22

    Is it possible (and if so how) to add an item to the column menu of a kendo UI grid?

  23. 23

    Auto incrementing column in Kendo UI Grid with ASP.NET MVC

  24. 24

    Kendo UI Grid local data source column sort by default

  25. 25

    How to mask phone number input in Kendo UI Grid Column

  26. 26

    Using templates&editors in grid column with Angular Kendo UI

  27. 27

    Use column.values together with row template in Kendo UI Grid

  28. 28

    escape JavaScript dot in kendo ui grid column field

  29. 29

    Kendo UI Grid handling missing values in column templates

HotTag

Archive