Telerik RadGrid GridDataItem - how to determine if column exists?
Asked Answered
A

2

6

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?

Annoy answered 7/11, 2013 at 16:10 Comment(0)
A
6

Will sent me on the right path:

var tableView = gridDataItem.OwnerTableView;
var gridColumn = tableView.Columns.FindByUniqueNameSafe(uniqueName);
if (gridColumn != null)
    {
        var cell = gridDataItem[gridColumn];
    ...
Annoy answered 8/11, 2013 at 0:40 Comment(0)
O
5

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");
    }
}
Oblivious answered 7/11, 2013 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.