Question

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?

Was it helpful?

Solution

Will sent me on the right path:

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

OTHER TIPS

Try using the RenderColumns collection:

protected void rgGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        bool found  = (from d in rgGrid.MasterTableView.RenderColumns select d).Any(d => d.UniqueName == "ColumnUniqueName");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top