Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telerik RadGrid GridDataItem - how to determine if column exists?

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?

like image 868
Lee Greco Avatar asked Nov 07 '13 16:11

Lee Greco


2 Answers

Will sent me on the right path:

var tableView = gridDataItem.OwnerTableView;
var gridColumn = tableView.Columns.FindByUniqueNameSafe(uniqueName);
if (gridColumn != null)
    {
        var cell = gridDataItem[gridColumn];
    ...
like image 86
Lee Greco Avatar answered Sep 19 '22 18:09

Lee Greco


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");
    }
}
like image 34
Will Avatar answered Sep 19 '22 18:09

Will