Address: http://www.star1168.com/blog/index.php/archives/158
Public Class Gridviewex: gridview { // Used to save Columns Private Arraylist _ columns; [defaultvalue ( True )] Public Bool Forceshowheader { Get { Object O = viewstate [" Forceshowheader "]; If (O! = Null ) Return ( Bool ) O; Return True ;} Set {Viewstate [" Forceshowheader "] = Value ;}} Protected Override Icollection createcolumns (pageddatasource datasource, Bool Usedatasource ){ If (_ Columns! = Null ) Return _ Columns; _ columns = (arraylist) Base . Createcolumns (datasource, usedatasource ); Return _ Columns ;} Protected Override Int Createchildcontrols (ienumerable datasource, Bool Databinding ){Int Count = Base . Createchildcontrols (datasource, databinding ); If (Forceshowheader & COUNT = 0) {table; If (Controls. Count = 0) {table = createchildtable (); controls. Add (table );} Else {Table = controls [0] As Table; If (Table = Null ) Throw New Applicationexception (" The first child control is not a table control. ");} Datacontrolfield [] fields = New Datacontrolfield [_ columns. Count]; _ columns. copyto (fields, 0 ); // Create a header and initialize this line Gridviewrow headerrow = createrow (-1,-1, datacontrolrowtype. header, datacontrolrowstate. Normal); initializerow (headerrow, fields ); // Raise the rowcreated event Gridviewroweventargs ARGs = New Gridviewroweventargs (headerrow); onrowcreated (ARGs ); // Add the header row to the table Table. Rows. addat (0, headerrow );} Return Count ;}}
Set the forceshowheader attribute to true (the default value is true ).