Continuing my ASP.NET GridView Tips and Tricks series, this post shows how to change the ASP.NET GridView Header Text at runtime.
I have seen many users using the RowCreated event, but I prefer the RowDataBound as it gives me more control, especially in cases where you need to change the values of Data bound controls.
Here’s the code to change the GridView HeaderText at Runtime. Let us first see how the GridView is declared
Now write the following code in the RowDataBound event to change the HeaderText of the 3rd column. Note that column index starts from zero.
protected void GridVie1_RowDataBound(object sender, GridViewRowEventArgs e) { // check for a condition if (1 == 1) { GridView1.Columns[2].HeaderText = "Price(10 Units)"; } }
Tweet
4 comments:
Is a really nice idea what you are dping!!! Thanks!
I tried your code to change the header text of my gridview on server side, after asigning datasourse and databind the model.... but Im getting an "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" error :/ any idea of what Im doing wrong?
Are you using DataKeys anywhere. What does GridView.DataKeys.Count result to?
I am also getting the same error....
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
>In window application. how to change the header text?
Post a Comment