Get HeaderText from a column in a GridView

3

When I go through the event RowDataBound of a GridView , I want to get the value of headertext of the column but I can not.

An example I want to get:

if (e.Row.RowType.ToString().Equals("Header"))
{           
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
    int tot = e.Row.Cells.Count;
    for (int i = 1; i < tot; i++)
    {
        TextBox txtValor = new TextBox();
        txtValor.Width = 15;
        string produ = e.Row.Header.text //Preciso de obter o valor do titulo da coluna.       
        txtValor.Text = (e.Row.DataItem as DataRowView).Row[produ].ToString();
        e.Row.Cells[i].Controls.Add(txtValor);
    }     
}
    
asked by anonymous 03.06.2014 / 13:50

1 answer

3

The following code returns you the column name of cell i (according to your comment // I need to get the column title value):

string produ = GridView1.HeaderRow.Cells[i].Text;

or an alternate form:

var cell = GridView1.HeaderRow.Cells[i] as DataControlFieldHeaderCell;
if (cell != null)
{
    string headerName = cell.ContainingField.HeaderText;
}

(replaced GridView1 by the name of your GridView )

    
03.06.2014 / 14:16