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

Accessing headertext from item_databound

1 Answer 290 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tami
Top achievements
Rank 1
Tami asked on 22 Sep 2010, 12:55 AM
protected void grdVerification_ItemDataBound(object sender, GridItemEventArgs e) 
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                string header = item.cells[0].HeaderText
            }
        }
Hi,
Is there a way to access the column header text when you're in the item_databound event?  I am autogenerating my columns and I won't know the names of the columns, but I need to be able to figure out which column I am in by name and not by index.  I would like to do something like the attached example.

Thanks!
Tami

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Sep 2010, 06:08 AM
Hello Tami,


If you want to access a specific cell in the grid row, then use AutoGeneratedColumns collection and use index to get corresponding UniqueName.

Code will look like this:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
     if (e.Item is GridDataItem)
     {
         GridDataItem item = (GridDataItem)e.Item;
         string un=  RadGrid1.MasterTableView.AutoGeneratedColumns[0].UniqueName; // get the uniquename
         string str = item[un].Text; // get the cell text
     }
}


In the case of you want to access the HeaderText, then, you need get reference to GridHeaderItem instead of GridDataItem, in ItemDataBound event. And can access each header text by column
Example:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
     if (e.Item is GridHeaderItem)
        {
            GridHeaderItem item = (GridHeaderItem)e.Item;
            string un = RadGrid1.MasterTableView.AutoGeneratedColumns[0].UniqueName; // get the uniquename of first column
            string header = item[un].Text;
        }
}

Also go through the documentation for more information on this.
Accessing cells and rows


Regards,
Shinu.
Tags
Grid
Asked by
Tami
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or