住所:
http://www.star1168.com/blog/index.php/archives/158 public class GridViewEx : GridView
{
// 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 table;
if (Controls.Count == 0)
{
table = CreateChildTable();
Controls.Add(table);
}
else
{
table = Controls[0] as Table;
if (table == null)
throw new ApplicationException(" Table ");
}
DataControlField[] fields = new DataControlField[_columns.Count];
_columns.CopyTo(fields, 0);
//
GridViewRow headerRow = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
InitializeRow(headerRow, fields);
// RowCreated
GridViewRowEventArgs args = new GridViewRowEventArgs(headerRow);
OnRowCreated(args);
//
table.Rows.AddAt(0, headerRow);
}
return count;
}
}
使用する場合は、ForceShowHeaderプロパティをtrueに設定すればよい(デフォルトはtrue