6

I want to change the header text of the gridview using Design from <TemplateField HeaderText="">.

I created a variable in code behind which is public and set the value in that variable and then I tried to call that variable over here as below:

<TemplateField HeaderText = '<%= VariableCallHere %>'

But while running the page, I got <%= VariableCallHere %> as a header text.

Even I tried changing using gridView1.HeaderRow.Cells[0].Text = "text Here" (This Throws object reference error)

Any one have any suggestions how this could be achieved?

5 Answers 5

10

It should be gridview1.Columns[ColumnIndex].HeaderText = "Header text";

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to set the header text based upon logic for the dataRows (after the Header row) and this did nothing but gridView1.HeaderRow.Cells[ColumnIndex].Text did work. I don't understand, but useful for anyone else struggling with this.
4

For this, in the RowDataBound event of the gridview control you need to write as like follows:

if (e.Row.RowType == DataControlRowType.Header)
{
  e.Row.Cells[0].Text = "column 1";
  e.Row.Cells[1].Text = "column 2";
  .....
}

Comments

0

I'm using it like this for multilingual it works fine NO need of extra work for iterating the rows just put it there and let it do the work

<asp:BoundField DataField="TITLE_NAME" HeaderText="<%$ Resources:Site,lblTitleName %>"
                            ItemStyle-Width="20%">
   <HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>

Comments

0

Access it via the columns collection:

gridview1.Columns[Index].HeaderText= "text Here";

As in:

gridview1.Columns[0].HeaderText= "text Here";

Comments

0
       if (e.Row.RowType == DataControlRowType.Header)
        {                
            Label lblAddInText = (Label)e.Row.FindControl("lblAddInText");
            lblAddInText.Text = "ADD IN TEXT" 
        }

in RowCreated Event of Gridview. Using and Label in .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.