This is a migrated thread and some comments may be shown as answers.

columns.AutoGenerate(true)´and Column names with spaces

3 Answers 894 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
Robert Madrian asked on 31 May 2017, 04:16 PM

Hello,

I have the following grid with columns.AutoGenerate(true) :

@(Html.Kendo().Grid<dynamic>()
      .Name("gridEQ")
      .Columns(columns =>
      {
          columns.AutoGenerate(true);
      })
      .Pageable()
      .Sortable()
      .Scrollable()
      .Selectable(s => s.Mode(GridSelectionMode.Multiple))
      .NoRecords()
      .Filterable(f => f.Enabled(false))
      .AutoBind(false)
      .DataSource(dataSource => dataSource
          .Ajax()
          .PageSize(100)
          .Read(read => read.Action("Read", "Home"))
      )
    )

 

If there is a query result from SQLServer with a column Name with spaces no rows are shown in the grid (the column Header is there)
see attached Picture

here is the json result:

{"Data":[{"Anzahl der Betriebsmittel":1376}],"Total":1,"AggregateResults":null,"Errors":null}

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 05 Jun 2017, 12:24 PM
Hello Robert,

Note that having a space in the name of a Model field is not valid. This is why there is an error thrown when the Grid tries to read the fields with space in them.

In order to resolve the error you can map the field names to a new object manually so that they do not include spaces. Furthermore, you can use PascalCase for the field names. This way when the columns are generated the Grid will split the field names and use them as a column title. 

On a side note, although having spaces in SQL field names is possible it is not recommended as it can cause issues down the road. If you would like additional information on best practices when setting column names you would find the article below interesting.



Regards,
Viktor Tachev
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
answered on 21 Nov 2017, 05:36 PM

Hello Viktor,

I now create my grid dynamic from a datatable (is availible in Core 2) but the same Problem with spaces in column names...

I have read in the following post (JQuery Grid) that it is possible to have such columns:
https://www.telerik.com/forums/grid-with-spaces-in-column-name

$("#grid").kendoGrid({
  columns: [ {
       field: '["Category Name"]',
       title: "Category Name"
  }]
 
});

 

Is this also possible with the MVC Version and how to set the Name (see below ?) for the column like the sample above?

columns.Bound(?).Title(column.ColumnName)

 

0
Accepted
Viktor Tachev
Telerik team
answered on 24 Nov 2017, 01:59 PM
Hi Robert,

You can pass a string to specify the name of the field that the column is bound to. For example:


columns.Bound("FieldName")

However, the string should correspond to a field in the Model that the Grid is bound to - usually that would be a field in the Model. If the fields in the database contain spaces you would need to corresponding fields in the Model with no spaces. Similar approach is also applicable for column names when building a DataTable. Check out the thread below that illustrates the approach.



Regards,
Viktor Tachev
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
Answers by
Viktor Tachev
Telerik team
Robert Madrian
Top achievements
Rank 1
Veteran
Iron
Share this question
or