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

Modify Grid Header in code behind

2 Answers 501 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Philip Senechal
Top achievements
Rank 1
Philip Senechal asked on 26 Jan 2011, 11:51 PM
I have a grid which I'm populating via pivot table in the code behind...therefore the columns are set to auto-generate.

I'd like to modify the header template so that the values are displayed as hyperlinks...I'll need to be able to programmatically create the hyperlink for each column using the header template text as a value.

Can you show me how to do this? I've gotten access to the header columns using the following Grid_PreRender event

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    RadGrid grid = ((RadGrid)sender);
 
    foreach (GridItem header in grid.MasterTableView.GetItems(GridItemType.Header))
    {
 
    }
}

I just don't know what I need to do in the foreach loop to change the header text to make it a hyperlink of the value.

Thanks for the help =)

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 27 Jan 2011, 05:31 AM
Hello Philip,

The following code snippet shows how to replace grid headertext with and HyperLink.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
       if (e.Item is GridHeaderItem)
         {
            GridHeaderItem item = (GridHeaderItem)e.Item;
            foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
            {
                HyperLink link = new HyperLink();
                link.Text = item[col.UniqueName].Text;
                link.NavigateUrl = "http://www.yoururl.com";//attach url
                item[col.UniqueName].Controls.Clear();
                item[col.UniqueName].Controls.Add(link);
            }
        }
    }

Thanks,
Princy.
0
Philip Senechal
Top achievements
Rank 1
answered on 03 Feb 2011, 07:13 AM
Hi Princy,

Thanks for the code...I finally got around to putting it in and it works perfectly!
Tags
Grid
Asked by
Philip Senechal
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Philip Senechal
Top achievements
Rank 1
Share this question
or