What is difference between GridView1.HeaderRow.Cells[4].Text and GridView1.Columns[4].HeaderText?

StackOverflow https://stackoverflow.com/questions/12119338

  •  28-06-2021
  •  | 
  •  

سؤال

I placed a Placeholder server control on aspx page.

create a Grid-view instance in code behind.

set data-source to it

Now i want to change it header text.so first i tried as given below:

GridView1.Columns[4].HeaderText = "ABC";

header text not changed.

then i tried with:

GridView1.HeaderRow.Cells[4].Text="ABC";

header text changed now.

Added GridView control to PlaceHolder server control:

plhGridView.Controls.Add(GridView1);

I want to know that why header text not changed when i tried first time?

Thanks

هل كانت مفيدة؟

المحلول

May be you are not binding columns with your Rows in your former code

GridView1.Columns[4].HeaderText = "ABC";

You can do this from the former code using RowDataBound handler for details use the following link:

ASP.NET: When and how to dynamically change Gridview's headerText's in code behind?

نصائح أخرى

If you want to try first time code. You have to set GridView1.AllowSorting="true"; and write OnSorting event for GridView.

GridView1.Columns[4].HeaderText = "ABC";

If what you want is:

GridView1.HeaderRow.Cells[4].Text="ABC";

This will keep the sort.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top