Gridview 网格视图asp.net显示标题(如果没有数据)
我想显示“网格视图”标题,即使绑定到网格的数据源为空?有没有办法在不添加空行的情况下实现同样的效果?最简单的方法是创建自己的GridView继承自Gridview 网格视图asp.net显示标题(如果没有数据),gridview,header,datasource,show,Gridview,Header,Datasource,Show,我想显示“网格视图”标题,即使绑定到网格的数据源为空?有没有办法在不添加空行的情况下实现同样的效果?最简单的方法是创建自己的GridView继承自GridView类。然后重写CreateChildControls方法以创建新的空表 像这样的方法应该会奏效: protected GridViewRow _footerRow2; protected GridViewRow _headerRow2; protected override int CreateChildControls(System.
GridView
类。然后重写CreateChildControls
方法以创建新的空表
像这样的方法应该会奏效:
protected GridViewRow _footerRow2;
protected GridViewRow _headerRow2;
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
// Call base method and get number of rows
int numRows = base.CreateChildControls(dataSource, dataBinding);
if (numRows == 0)
{
//no data rows created, create empty table
//create table
Table table = new Table();
table.ID = this.ID;
//convert the exisiting columns into an array and initialize
DataControlField[] fields = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(fields, 0);
if (this.ShowHeader)
{
//create a new header row
_headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
this.InitializeRow(_headerRow2, fields);
_headerRow2.EnableTheming = true;
table.Rows.Add(_headerRow2);
}
if (this.ShowFooter)
{
//create footer row
_footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
this.InitializeRow(_footerRow2, fields);
_footerRow2.EnableTheming = true;
table.Rows.Add(_footerRow2);
}
this.Controls.Clear();
this.Controls.Add(table);
}
return numRows;
}
基本上,检查GridView是否有行,如果没有行,则创建页眉行和页脚行(如果它们已启用)
编辑:
此外,如果您希望仍然显示EmptyDataText,可以在创建页眉和页脚之间添加这些行
GridViewRow emptyRow;
if (this.EmptyDataTemplate != null)
{
emptyRow = this.Controls[0].Controls[0] as GridViewRow;
}
table.Rows.Add(emptyRow);
最简单的方法是创建自己的GridView,继承自
GridView
类。然后重写CreateChildControls
方法以创建新的空表
像这样的方法应该会奏效:
protected GridViewRow _footerRow2;
protected GridViewRow _headerRow2;
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
// Call base method and get number of rows
int numRows = base.CreateChildControls(dataSource, dataBinding);
if (numRows == 0)
{
//no data rows created, create empty table
//create table
Table table = new Table();
table.ID = this.ID;
//convert the exisiting columns into an array and initialize
DataControlField[] fields = new DataControlField[this.Columns.Count];
this.Columns.CopyTo(fields, 0);
if (this.ShowHeader)
{
//create a new header row
_headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
this.InitializeRow(_headerRow2, fields);
_headerRow2.EnableTheming = true;
table.Rows.Add(_headerRow2);
}
if (this.ShowFooter)
{
//create footer row
_footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
this.InitializeRow(_footerRow2, fields);
_footerRow2.EnableTheming = true;
table.Rows.Add(_footerRow2);
}
this.Controls.Clear();
this.Controls.Add(table);
}
return numRows;
}
基本上,检查GridView是否有行,如果没有行,则创建页眉行和页脚行(如果它们已启用)
编辑:
此外,如果您希望仍然显示EmptyDataText,可以在创建页眉和页脚之间添加这些行
GridViewRow emptyRow;
if (this.EmptyDataTemplate != null)
{
emptyRow = this.Controls[0].Controls[0] as GridViewRow;
}
table.Rows.Add(emptyRow);
从ASP.NET 4开始,您可以将GridView的
ShowHeaderWhenEmpty
属性设置为true
从ASP.NET 4开始,您可以将GridView的ShowHeaderWhenEmpty
属性设置为true
出于什么原因这样做…仅显示带有标题的空GridView?如果数据源为空,用户必须查看空白区域(可能有“未找到项目”的文本)。他/她不能理解,我同意。有时,即使没有数据,也可以显示列标题。出于什么原因,您希望这样做…仅显示标题为空的gridview?如果数据源为空,用户必须查看空白区域(可能包含文本“未找到项”)。他/她不能理解,我同意。有时,即使没有数据,也可以显示列标题。