ASP.NET GridView header row text empty when AllowSorting enabled

asp.net

Solution

It's because when the gridview is sortable a LinkButton is used.

You need something like:

LinkButton Link = GridView1.HeaderRow.Cells[0].Controls[0] as LinkButton;

String Title = Link.Text;

Problem

I am solving interesting problem. I have a gridview in my application and after button click (in OnClick event) I am trying to get selected row and header row. All worked fine when the grid was not sortable (AllowSorting="false"). This is the way how I access the header row cells: ``` GridViewControl.HeaderRow.Cells[idx].Text ``` But, once I switched on sorting (AllowSorting="True"), this approach does not work. Header row is created, expression GridViewControl.HeaderRow.Cells.Count returns the correct number of columns, but GridViewControl.HeaderRow.Cells[idx].Text property is always EMPTY! Nevertheless, on the page the grid view is displayed correctly including header column texts ... I have tried to find the answer on the web but without success ... I have found some similar questions but never answered ... so, does anybody know how to get header row column texts when sorting is enabled? Thank you in advance. PS: Do not advice me using of ``` GridVewControl.Columns[idx].Text ``` property ... there are just 2 columns with Edit & Select commands ... rest columns are automatically generated.

Original source