Reference different column in grid bound to DataTable

2 posts, 0 answers
  1. Katia
    Katia avatar
    5 posts
    Member since:
    Mar 2008

    Posted 05 Feb 2015 Link to this post

    Hi,

    I'm binding my grid to a DataTable for displaying dynamic search results. I've followed the example on how to do binding with DataTable, but I was wondering if there is a way to reference a different column in the Template / ClientTemplate. I've tried what I regularly do when bound to a Model/ViewModel, but it doesn't seem to work for DataTables. I've tried both

    This is what my grid looks like:

    @(Html.Kendo().Grid(Model.Result.Data)
        .Name("gridSearchResults")
        .Columns(columns =>
        {
            foreach (System.Data.DataColumn column in Model.Result.Data.Columns)
            {
                if (column.ColumnName != "Id")
                {
                    if (column.ColumnName == "Name")
                    {
                        columns.Bound("Name").Template(@<text><a href='" + Url.Action("Details", Model.Result.DataSource) + "/#= Id #'" + ">#= Name #</a></text>);
                    }
                    else
                    {
                        columns.Bound(column.ColumnName);
                    }
                }
            }
        })
        .DataSource(d => d
            .Server()
            .Model(m => {
                m.Id("Id");
                foreach (System.Data.DataColumn column in Model.Result.Data.Columns)
                {
                    m.Field(column.ColumnName, column.DataType);
                }
            })
        )
    )

    Model.Result.Data is of type DataTable. It renders all the columns fine, but I'd like the name column to be a hyperlink to the details page.. Any suggestions?

    Thank you 
  2. Katia
    Katia avatar
    5 posts
    Member since:
    Mar 2008

    Posted 05 Feb 2015 in reply to Katia Link to this post

    I guess I should have tried some more variations of my Template format. I found a post mentioning you can use @item which will refer to the current row dataitem, in this case a DataRowView and I was able to reference the column values I needed.

    The following works:

    columns.Bound("Name").Template(@<text>@Html.ActionLink((string)@item.Row["Name"], "Details", Model.Result.DataSource, new { Id = @item.Row["Id"] }, null)</text>);


Back to Top