问题
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.
回答1:
I solved it using:
string headerRowText = gvList.HeaderRow.Cells[i].Text;
回答2:
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.
回答3:
string headerRowText = gvList.HeaderRow.Cells[i].Text;
returned empty string for me, what did work was:
GridView1.Columns[i].HeaderText
回答4:
Set GridView property UseAccessibleHeaderText=true
Then, on code-behind, to get j-th column use:
GridView1.HeaderRow.Cells[0].Text
来源:https://stackoverflow.com/questions/2091202/get-header-text-of-gridview-cell