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

Cannot access cells in OnItemDataBound

2 Answers 67 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Islam
Top achievements
Rank 1
Islam asked on 17 Feb 2009, 02:37 PM
Hi,
I want to access the cells of the current row in the ItemDataBound event handler.

This throws exception
void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            GridDataItem item = e.Item as GridDataItem;
            //item["Naam"].Text     --throws exception, my column is not found

            DataRowView row = item.DataItem as DataRowView;
    //row[0]  --works ok
        }

What I need to do the to change the color of the ForeColor property of the cell I want to access, Is there a way to access a specified cell with its index or even column unique name?

2 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 17 Feb 2009, 04:32 PM
Hello Islam,

Please test the following modification:
void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e) 
    if(e.Item is GridDataItem) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        string str = item["Naam"].Text; 
    } 

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Shinu
Top achievements
Rank 2
answered on 18 Feb 2009, 04:24 AM
Hi Islam,

I used to get this error with Hierarchical Grid. This error is raised when I expand the master table and the column that I am trying to access may not be present in the Detail table. Since the ItemDataBound event gets raised at whatever level of the RadGrid's
tables are getting bound, we need some kind of check to make sure which table(Master or detail) we're dealing with. Otherwise we will get an error when trying to locate the " " field in the data item. If this is the case with you, you may also try setting the Name property of the Master and Detail table.

ASPX:
 
<MasterTableView  Name="Master"  > 

CS:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
      if((e.Item is GridDataItem)&&(e.Item.OwnerTableView.Name=="Master"))  
      {  
        GridDataItem item = (GridDataItem)e.Item;  
        string str = item["Naam"].Text;  
      }  
  } 


Regards
Shinu.

Tags
Grid
Asked by
Islam
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Shinu
Top achievements
Rank 2
Share this question
or