hotcoder
hotcoder

Reputation: 3256

Get Header Text of Gridview Cell

I've a gridview in my web form and I'm using a the following code in my web form's Save button:

foreach (GridViewRow row in gvList.Rows)
            if (row.RowType == DataControlRowType.DataRow)
            {  for (int i = 0; i < row.Cells.Count; i++)
                {
                    string headerRowText = ???;

How can I get the current cell's header text.

Upvotes: 10

Views: 50773

Answers (4)

alessalessio
alessalessio

Reputation: 1234

Set GridView property UseAccessibleHeaderText=true

Then, on code-behind, to get j-th column use:

GridView1.HeaderRow.Cells[0].Text

Upvotes: 1

BornToCode
BornToCode

Reputation: 10223

string headerRowText = gvList.HeaderRow.Cells[i].Text;

returned empty string for me, what did work was:

GridView1.Columns[i].HeaderText

Upvotes: 6

hotcoder
hotcoder

Reputation: 3256

I solved it using:

  string headerRowText = gvList.HeaderRow.Cells[i].Text;

Upvotes: 25

Joel Etherton
Joel Etherton

Reputation: 37543

gvList.Rows[0] should be your header row. You should be able to get

gvList.Rows[0].Cells[i]

That's just to get the cell itself. You'll need to go into the cell and get Controls[0] and cast it to its proper type then get the Text property.

Upvotes: 4

Related Questions