Kendo MVC Grid Transpose Columns using HTML Helper

1 Answer 18 Views
Grid
Priya
Top achievements
Rank 1
Priya asked on 20 May 2025, 10:21 AM

Hi,

I'm trying to figure out how to transpose columns in a Kendo Grid using the Html helper. 

I see that there's this small piece of documentation for jQuery (https://docs.telerik.com/kendo-ui/knowledge-base/transposed-grid), but would ideally like to do it on the helper itself.

Is anyone aware of how to do this? 

Any help is much appreciated!

1 Answer, 1 is accepted

Sort by
1
Accepted
Mihaela
Telerik team
answered on 23 May 2025, 07:26 AM

Hello Priya,

Indeed, you can use the same approach when usng HtmlHelper Grid:

  • Within the $(document).ready() function,  trigger an Ajax request to get all of the data and transpose it, as described in the KB article. Then, create a new DataSource instance with jQuery and assing the transposed data collection to it. use the setDataSource() method to set the new DataSource to the Grid.
  • It is important to point out that the declaration of the columns in the Grid should not be set.
@(Html.Kendo().Grid<OrderViewModel>()    
    .Name("grid")
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .HtmlAttributes(new { style = "height:430px;" })
)

<script>
    var rawData,
        data = [],
        model = {},
        length,
        dataLength,
        propertiesLength;

$(document).ready(function() {
    // AJAX request to retrieve the data
    ...

    // trnaspose the data
    dataLength = rawData.length;
    propertiesLength = Object.keys(rawData[0]).length;

    for(var i=0; i <propertiesLength; i+=1){
        data[i] = {};
        for(var j =0; j < dataLength; j+=1 ){
            var currentItem = rawData[j]
            var property = Object.keys(currentItem)[i];
            if(j === 0){
                data[i]["Property"] = property;
            }
            data[i][currentItem.FirstName] = currentItem[property]
        }
    }

    var dataSource = new kendo.data.DataSource({
        data: data,
        pageSize: 20
    });

    var grid = $("#grid").data("kendoGrid");
    grid.setDataSource(dataSource); // set the DataSource instance
});
</script>

If you have any other queries, you are more than welcome to let me know.

Regards,
Mihaela
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Priya
Top achievements
Rank 1
commented on 28 May 2025, 10:22 AM

Hi Mihaela,

Many thanks for your response. Ah, I understand - you apply the jQuery after you establish the grid in the page.

One issue I was getting was related to dataLength.

Instead, I had to reference 'Data' property of rawData:

dataLength = rawData.Data.length;
propertiesLength = Object.keys(rawData.Data[0]).length;

Also, what's going on here cos I'm getting an error that 'Quarter' doesn't exist:

data[i][currentItem.FirstName] = currentItem[property]

For reference, this is what my grid looks like:

Thank you,

Priya

 

Priya
Top achievements
Rank 1
commented on 28 May 2025, 11:29 AM | edited

I figured it out!

Because I added ClientTemplate to my Quarter column, it was reading incorrectly.

Just going to do some touch ups now.

Mihaela
Telerik team
commented on 30 May 2025, 05:54 AM

Thank you for your update, Priya, It is great that you managed to resolve the issue.

On a separate note, I would like to mention that you can instantiate a trial license, particularly for the Telerik UI for ASP.NET MVC suite. This will enable you to receive timely support and open separate ticket threads that will be processed through our internal support system as well. 

If you have any questions or concerns, please let me know.

Best,
Mihaela

Tags
Grid
Asked by
Priya
Top achievements
Rank 1
Answers by
Mihaela
Telerik team
Share this question
or