Search code examples
c#asp.netheadergridfindcontrol

Find header value for a cell in a grid


I have gridview, I want to do a foreach on it's rows and get the value from column's header where there's a Label for one cell from this row.

foreach (GridViewRow mainRow in grid1.Rows)
{
    var header = mainRow.Cells[2].Parent.FindControl("LabelID");//is null
}

How do I find it ?


Solution

  • If you want the value in RowDataBound event then you can check RowType like this

    if(e.Row.RowType == DataControlRowType.Header)
    {
        Label header = (Label)e.Row.FindControl("LabelID");
    }