我制作了一个扩展 gridview 的类,以将使用时生成autogeneratecolumns=true
的列添加到列集合中。
using System;
using System.Data;
using System.Collections;
using System.Web.UI.WebControls;
public class GridViewEx1 : GridView
{
private DataControlFieldCollection originalColumns;
public GridViewEx1()
: base()
{
}
public void RecordColumns()
{
originalColumns = new DataControlFieldCollection();
foreach (DataControlField column in Columns)
originalColumns.Add(column as DataControlField);
}
public void ResetColumns()
{
if (originalColumns == null)
RecordColumns();
else
{
Columns.Clear();
foreach (DataControlField column in originalColumns)
Columns.Add(column as DataControlField);
}
}
protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
{
ResetColumns();
ICollection generatedColumns = base.CreateColumns(dataSource, useDataSource);
foreach (DataControlField column in generatedColumns)
if (!originalColumns.Contains(column))
Columns.Add(column as DataControlField);
return Columns;
}
}
但我不知道如何在 asp.net 页面中使用此控件?
我尝试使用我原来的 gridview 并像这样初始化它
gv1 = new GridViewEx1(); // gv1 is the id of my gridview
但这只会使我的 gridview 完全为空。