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

[Solved] How to get the value of a cell when the columns are dynamically generated?

3 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paul Hoeffer
Top achievements
Rank 1
Paul Hoeffer asked on 15 Nov 2010, 08:37 PM
I'm trying to understand how to get the contents of a cell in grid where the columns are dynamically generated.

For instance, if I want to change the style of a cell to reflect that of it's contents like:

.CellAction(cell => {

    cell.HtmlAttributes[

 

"style"] = "cls-rankings-" + cell.Text;

 

})

the contents of cell.text are always empty.  Since the columns come from a DataTable I can't hard code them, e.g. cell.DataItem.SomeColumnName

Is there anyway to do this? It seems like it should be trivial - what am I missing?

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 16 Nov 2010, 08:54 AM
Hi Paul,

 As you are using DataTable for a grid's data source you can very easily get a given data item's field value by current column's member name.

<%=Html.Telerik().Grid(Model).Name("Grid")// Note: the Model is of type DataTable
      .CellAction(cell => cell.HtmlAttributes["class"] = "cls-rankings-" + cell.DataItem[cell.Column.Member])
%>


All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Paul Hoeffer
Top achievements
Rank 1
answered on 16 Nov 2010, 05:38 PM
Perfect!!

Quick question, what if the Model is a custom type, e.g. class Foo with 5 properties.  Is there any way to access that generically as well w/o having to specify the property name?

Cheers,
0
Rosen
Telerik team
answered on 17 Nov 2010, 08:00 AM
Hi Paul,

You may consider using reflection or construct a lambda expression tree to extract the value.

Best wishes,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Paul Hoeffer
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Paul Hoeffer
Top achievements
Rank 1
Share this question
or