如果Gridview 的DataSouce is null,绑定时是一片白,
目前要做到如果数据源为空时.1 要显示列头,
2 Gridview 显示5行空表格.
请大侠指点.网上找的代码,只能完成1,我还不知道怎么用,菜了.
///
/// GridView 扩展控件
/// @author:[email protected]
///
public class GridView : System.Web.UI.WebControls.GridView
{
private bool _enableEmptyContentRender = true ;
///
/// 是否数据为空时显示标题行
///
public bool EnableEmptyContentRender
{
set { _enableEmptyContentRender = value; }
get { return _enableEmptyContentRender; }
}
private string _EmptyDataCellCssClass ;
///
/// 为空时信息单元格样式类
///
public string EmptyDataCellCssClass
{
set { _EmptyDataCellCssClass = value ; }
get { return _EmptyDataCellCssClass ; }
}
///
/// 为空时输出内容
///
///
protected virtual void RenderEmptyContent(HtmlTextWriter writer)
{
Table t = new Table(); //create a table
t.CssClass = this.CssClass; //copy all property
t.GridLines = this.GridLines;
t.BorderStyle = this.BorderStyle;
t.BorderWidth = this.BorderWidth;
t.CellPadding = this.CellPadding;
t.CellSpacing = this.CellSpacing;
t.HorizontalAlign = this.HorizontalAlign;
t.Width = this.Width;
t.CopyBaseAttributes(this);
TableRow row = new TableRow();
t.Rows.Add(row);
foreach (DataControlField f in this.Columns) //generate table header
{
TableCell cell = new TableCell();
cell.Text = f.HeaderText;
cell.CssClass = "TdHeaderStyle1"; //这里把表头样式写死了
row.Cells.Add(cell);
}
TableRow row2 = new TableRow();
t.Rows.Add(row2);
TableCell msgCell = new TableCell();
msgCell.CssClass = this._EmptyDataCellCssClass;
if (this.EmptyDataTemplate != null) //the second row, use the template
{
this.EmptyDataTemplate.InstantiateIn(msgCell);
}
else //the second row, use the EmptyDataText
目前要做到如果数据源为空时.1 要显示列头,
2 Gridview 显示5行空表格.
请大侠指点.网上找的代码,只能完成1,我还不知道怎么用,菜了.
///
/// GridView 扩展控件
/// @author:[email protected]
///
public class GridView : System.Web.UI.WebControls.GridView
{
private bool _enableEmptyContentRender = true ;
///
/// 是否数据为空时显示标题行
///
public bool EnableEmptyContentRender
{
set { _enableEmptyContentRender = value; }
get { return _enableEmptyContentRender; }
}
private string _EmptyDataCellCssClass ;
///
/// 为空时信息单元格样式类
///
public string EmptyDataCellCssClass
{
set { _EmptyDataCellCssClass = value ; }
get { return _EmptyDataCellCssClass ; }
}
///
/// 为空时输出内容
///
///
protected virtual void RenderEmptyContent(HtmlTextWriter writer)
{
Table t = new Table(); //create a table
t.CssClass = this.CssClass; //copy all property
t.GridLines = this.GridLines;
t.BorderStyle = this.BorderStyle;
t.BorderWidth = this.BorderWidth;
t.CellPadding = this.CellPadding;
t.CellSpacing = this.CellSpacing;
t.HorizontalAlign = this.HorizontalAlign;
t.Width = this.Width;
t.CopyBaseAttributes(this);
TableRow row = new TableRow();
t.Rows.Add(row);
foreach (DataControlField f in this.Columns) //generate table header
{
TableCell cell = new TableCell();
cell.Text = f.HeaderText;
cell.CssClass = "TdHeaderStyle1"; //这里把表头样式写死了
row.Cells.Add(cell);
}
TableRow row2 = new TableRow();
t.Rows.Add(row2);
TableCell msgCell = new TableCell();
msgCell.CssClass = this._EmptyDataCellCssClass;
if (this.EmptyDataTemplate != null) //the second row, use the template
{
this.EmptyDataTemplate.InstantiateIn(msgCell);
}
else //the second row, use the EmptyDataText
中加入你想要的东东!!
DataTable dTable = new DataTable ();
dTable.Columns .Add ("列名1",typeof(string));
dTable.Columns .Add ("列名2",typeof(string));
dTable.Columns .Add ("列名3",typeof(string));
for(int i=0;i<5;i++)
{
DataRow dr = dTable.NewRow ();
dr["列名1"] = "" ;
dr["列名2"] = "" ;
dr["列名3"] = "" ;
dTable.Rows .Add (dr);
}
this.DataGrid1.DataSource =dTable;
this.DataGrid1.DataBind();
就可以保存样式和标题~`~~
<EmptyDataTemplate>
//这里写你在无数据时要显示的样式
如果只要表头
<table>
内容
</table>
</EmptyDataTemplate>