Get GridView column header text - always returns empty

I have a GridView from which I would like to get the text of the column headers.

for (int j = 0; j < grdToDisplay.HeaderRow.Cells.Count; j++) { string s = grdToDisplay.HeaderRow.Cells[j].Text.ToString(); //Returns "" s = grdToDisplay.HeaderRow.Cells[j].Text; //Returns "" s = grdToDisplay.Rows[0].Cells[j].Text; //Returns text from first row of results not the header // s = grdToDisplay.Columns[j].HeaderText.ToString(); // does not work as column count is 0 } 

GridView content is generated at runtime based on a user request. The header is sorted by click.

How can I loop a GridView and enumerate the text of the column heading?

+6
source share
5 answers

You can use GridViewColumn.HeaderText

 for (int i = 0; i < grdToDisplay.Columns.Count; i++) { string header = grdToDisplay.Columns[i].HeaderText; } 

Edit :

but this will not give me any results since the number of columns is 0

Then you have AutoGenerateColumns=true and only declaratively added columns are counted. So use this code after , you have a binding to the GridView :

 for (int i = 0; i < grdToDisplay.HeaderRow.Cells.Count; i++) { string header = grdToDisplay.HeaderRow.Cells[i].Text; } 
+4
source

Since the GridView title is sorted, you can get the title text from Linkbutton. Try this: put the following code in the DataBind for the gridView:

 for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++) { if (gridView.HeaderRow.Cells[i].HasControls()) { //when gridview is sortable, type of header is LinkButton // Linkbutton is in index 0 of the control if (gridView.HeaderRow.Cells[i].Controls[0] is LinkButton) { LinkButton headerControl = gridView.HeaderRow.Cells[i].Controls[0] as LinkButton; string headerName = headerControl.Text; } } } 
+2
source

I created my own solution based on the things that I read here and in other topics .. here it goes:

 public void HideColumnByName(GridView grid, string header) { if (grid.HeaderRow.HasControls()==true) { for (int i = 0; i < grid.HeaderRow.Cells.Count; i++) { if (grid.HeaderRow.Cells[i].Text == header) { foreach (GridViewRow row in grid.Rows) { row.Cells[i].Visible = false; grid.HeaderRow.Cells[i].Visible = false; } } } } } 

this method directly hides both headers and column cells, the header name (or column name) is the string parameter passed to my method (the 'header' parameter). Then the "HideColumnByName" method is called from the "DataBound" event of my gridview. Just. Hope this helps! It really helped me! :)

0
source

I recently ran into this problem while doing some recent coding, anytime when I tried to get the header text, it would always return empty even with related events. I just came up with a solution when I decided to try converting the header based on my own column management scheme.

It turns out that gridview sorting turns the header text into something else, which makes it impossible to simply get information from the header without converting it. My own solution to the problem was simply not to sort it when I requested the text, but, believing that this was not possible, I came across a solution to get the text even when it is sorted in another thread:

ASP.NET GridView title bar text empty if allowed

0
source

enter image description here The header text returns the name specified in the source column if the base of the template column is on the data source

  • GridView.Columns (i) .HeaderText

but if you need to get the header text for the column, first use this line

  • GridView.HeaderRow.Cells (i) .Text

this link can help you get the Gridview cell title text

0
source

All Articles