Get Headertext from a column in a Gridview

Asked

Viewed 1,373 times

3

Going through the event RowDataBound of a GridView I intend to obtain the value of headertext column but I’m not getting.

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);
    }     
}

1 answer

3


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

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

or an alternative form:

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

(replaced Gridview1 by the name of his GridView)

  • Returns empty, returns no value

  • @user6018 I’ll assume you’re filling gridview through the database, maybe that’s why it’s appearing empty. I edited the answer with another solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.