jimmy
jimmy

Reputation: 756

Get the GridView Column Header Text - always returns blank

I have a GridView that I would like to get the Text of the columns headers from.

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
}

The GridView contents are generated at runtime based on a user query. The header is click-able to sort.

How can I loop the GridView and list the column header text?

Upvotes: 7

Views: 30962

Answers (6)

Code
Code

Reputation: 739

grdExcel.HeaderRow.Cells[0].Text.ToString();

Upvotes: 0

Mostafa Anssary
Mostafa Anssary

Reputation: 135

enter image description here
the header text return the filed name if template column base on data-source

  • GridView.Columns(i).HeaderText

but if what you need to get header text for column first use that line

  • GridView.HeaderRow.Cells(i).Text

this link may help you Get Header Text of Gridview Cell

Upvotes: 0

ShadowX
ShadowX

Reputation: 1

I recently came across this very problem when doing some recent coding, anytime I tried getting the header text it would always return blank even on data bound events. I only came to the solution when I thought to try converting the header based on the columns own control scheme.

It turns out, making a gridview sortable turns the header text into something else, making it impossible to simply get the information from the header without converting it. My own solution to the problem was to simply have it not be sortable when I asked for the text, but assuming that isn't possible I did come across the solution to getting the text even while it is sortable in another thread:

ASP.NET GridView header row text empty when AllowSorting enabled

Upvotes: 0

FaKu Soler
FaKu Soler

Reputation: 1

I created my own solution based on things i´ve read here and in other threads .. 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;
                        }
                    }

                }
            }
        }

that method straight up hides both Headers and Cells of the column which header name (or column name) is the string parameter passed to my method ('header' param). "HideColumnByName" method is then called from the "DataBound" event of my gridview. Simple. Hope this helps ! It sure did work for me ! :)

Upvotes: 0

Parsa
Parsa

Reputation: 1309

Since header of GridView is sortable, you can get the text of the header from Linkbutton. try this one: put the following code in the DataBind of 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;

                    }

                }

            }

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460028

You can use GridViewColumn.HeaderText

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

Edit:

but this would give me no results as the columns count is 0

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

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

Upvotes: 5

Related Questions