# Displaying Chart in a Column of a Grid

## Environment

<table>
    <tr>
    <td>Product</td>
    <td>Telerik UI for ASP.NET MVC Chart</td>
    </tr>
    <tr>
    <td>Product Version</td>
    <td>Created with version 2024.4.1112</td>
    </tr>
</table>

## Description

How can I initialize a Chart into a specified [Grid](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview) column?

## Solution

1. Declare the Grid component.
2. Use the [`Template()`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/gridcolumnfactory#templatesystemstring) option of the Grid column to define the Charts.
3. Handle the [`DataBinding`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/grideventbuilder#databindingsystemstring) and [`DataBound`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/grideventbuilder#databoundsystemstring) events of the Grid.
4. Within the event handlers, call the [`destroy`](https://docs.telerik.com/kendo-ui/api/kendo/methods/destroy) method and the jQuery [`eval`](https://www.w3schools.com/jsref/jsref_eval.asp#:~:text=The%20eval\(\)%20method%20evaluates,eval\(\)%20executes%20the%20statements.) method to render the Charts. By design, the script tags are not automatically evaluated inside the Grid column template, so the scripts must be evaluated manually in the `DataBound` event of the Grid..

```Razor HtmlHelper
    @model IEnumerable<ViewModel>

    @(Html.Kendo().Grid<ViewModel>(Model)
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(v => v.ID);
            columns.Template(@<text>
                @(Html.Kendo().Chart<ChartItem>()
                    .Name("chart_#=ID#") 
                    .SeriesDefaults(defaults => defaults.Column().Stack(true))
                    .DataSource(dataSource => dataSource
                        .Read(read => read.Action("ReadChartData", "Home").Data("function() { return { id: #=ID# }; }"))
                    )
                    .Series(series =>
                    {
                        series.Column(s => s.Value).CategoryField("Category");
                        series.Column(s => s.Value1).CategoryField("Category");
                    })
                    .Tooltip(tooltip => tooltip.Template("\\#: category \\# - \\#: value \\#").Visible(true))
                    .ToClientTemplate())
            </text>).Title("Chart Remote Data");
            columns.Template(@<text>
                @(Html.Kendo().Chart<ChartItem>()
                    .Name("local_#=ID#") 
                    .HtmlAttributes(new { @class = "chart-local" })
                    .SeriesDefaults(defaults => defaults.Column().Stack(true))
                    .Series(series =>
                    {
                        series.Column(s => s.Value).CategoryField("Category");
                        series.Column(s => s.Value1).CategoryField("Category");
                    })
                    .ToClientTemplate())
            </text>).Title("Chart Local Data");
        })
        .Events(e => e.DataBinding("onDataBinding").DataBound("onDataBound"))
        .DataSource(dataSource => dataSource
            .Ajax()
        )
    )
```

```JavaScript
<script>
    function onDataBinding() {
        kendo.destroy(this.tbody);
    }

    function onDataBound() {
        var grid = this;
        grid.tbody.find("script").each(function () {
            eval($(this).html());
        });
        $(function () {
            grid.tbody.children().each(function () {
                var item = grid.dataItem(this);
                $(this).find(".chart-local").data("kendoChart").dataSource.data(item.ChartData);
            });
        });
    }
</script>
```

```CSS Styles
  <style>
        #grid .k-chart {
        height: 150px;
    }
  </style>
```

For the complete implementation of the suggested approach, refer to [the project on how to use a Chart in the ClientTemplate of a Grid column and bind the Chart based on the row data](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/ChartInGrid).

## More ASP.NET MVC Chart Resources

* [ASP.NET MVC Chart Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/charts/overview)

* [ASP.NET MVC Chart Demos](https://demos.telerik.com/aspnet-mvc/charts/index)

* [ASP.NET MVC Chart Product Page](https://www.telerik.com/aspnet-mvc/charts)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Chart for ASP.NET MVC](https://www.telerik.com/kendo-jquery-ui/documentation/api/dataviz/ui/chart)

* [Telerik UI for ASP.NET MVC Grid Templates](https://docs.telerik.com/aspnet-core/html-helpers/data-management/grid/templates/overview)

* [Server-Side API Reference of the Chart for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/chart)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
