Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET GridView header row text empty when AllowSorting enabled

Tags:

asp.net

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.

like image 918
12moyo34 Avatar asked Jan 14 '23 12:01

12moyo34


1 Answers

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;
like image 148
Steve Wellens Avatar answered Jan 18 '23 23:01

Steve Wellens