Telerik RadGrid GridDataItem - how to determine if column exists?

Lee Greco picture Lee Greco · Nov 7, 2013 · Viewed 9.7k times · Source

I'm using a base class to modify the behavior of any Telerik RadGrid that appears on my ASP.Net pages. In the base class, I want to perform certain operations (set Css, tool tips, etc) on many common columns, but not every common column exists in every grid.

In the ItemDataBound event I'm getting an instance to the GridDataItem and in turn I want to get a reference to one or more of the contained cells of the GridDataItem:

var cell = gridDataItem["ColumnUniqueName"]

Problem is that this throws a GridException if the named column doesn't exist:

Cannot find a cell bound to column name 'ColumnUniqueName'

Is there a way to test for existence of a column by name before referencing it or am I stuck using try catch?

Answer

Lee Greco picture Lee Greco · Nov 8, 2013

Will sent me on the right path:

var tableView = gridDataItem.OwnerTableView;
var gridColumn = tableView.Columns.FindByUniqueNameSafe(uniqueName);
if (gridColumn != null)
    {
        var cell = gridDataItem[gridColumn];
    ...