I have a gridview that is generating columns automatically (having a List<> DataSource), now what I need is to specify HeaderText for it’s columns. I’ve googled a lot and checked some previous questions only to find that autogenerated grids doesn’t have Columns Collection, at last I created a custom grid of my own following this article http://damieng.com/blog/2006/09/25/extending-gridview-to-access-generated-columns
now I am getting the column collection in a property BoundColumns, I have tried setting HeaderText property on PageLoad, RowDataBound events but all in vain, I need to know on which event I can put this code to make it work
gridUsers.BoundColumns[0].Visible = false;
gridUsers.BoundColumns[1].HeaderText = "Login ID";
gridUsers.BoundColumns[2].HeaderText = "User Name";
gridUsers.BoundColumns[3].Visible = false;
gridUsers.BoundColumns[4].HeaderText = "User Email";
gridUsers.BoundColumns[5].HeaderText = "User Status";
gridUsers.BoundColumns[6].HeaderText = "User Role";
P.S: Visible properties are working on Page Load. Any Help is appreciated.
You don’t have to do an auto generated column especially if you want to set the headers manually. Either change the data source to the return with the correct column headers or use the
<columns><asp:boundfield HeaderText="" >...and specify the columns manually (and setAutoGenerateColumns=false).In most cases, you alter the header and column during RowDataBound event but you don’t need to if you do declarative syntax or unless you have special rendering requirements.