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

Reference different column in grid bound to DataTable

1 Answer 187 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Katia
Top achievements
Rank 1
Katia asked on 06 Feb 2015, 12:43 AM
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 

1 Answer, 1 is accepted

Sort by
0
Katia
Top achievements
Rank 1
answered on 06 Feb 2015, 01:09 AM
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>);


Tags
Grid
Asked by
Katia
Top achievements
Rank 1
Answers by
Katia
Top achievements
Rank 1
Share this question
or