重写gridview控件 当数据源为空时 怎么显示footer行
重写了gridview控件 当数据源为空的时候 重写了表头
protected override void Render(HtmlTextWriter writer)
{
if (this.Rows.Count == 0 || this.Rows[0].RowType == DataControlRowType.EmptyDataRow)
{
RenderEmptyContent(writer);
}
else
{
base.Render(writer);
}
}
protected virtual void RenderEmptyContent(HtmlTextWriter writer)
{
Table t = new Table();
t.CssClass = this.CssClass;
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)
{
TableHeaderCell cell = new TableHeaderCell();
cell.Text = f.HeaderText;
row.Cells.Add(cell);
}
t.RenderControl(writer);
}
可是这样footer行显示不出来 有么有什么好的方法 不想采用插入空行再删除的方式 太麻烦
谁能指导一下 谢谢
[最优解释]
那在循环里可以改成这样:
foreach (DataControlField f in this.Columns)
{
TableHeaderCell cell = new TableHeaderCell();
cell.Text = f.HeaderText;
headerRow.Cells.Add(cell);
var cell1 = new DataControlFieldCell(f);
cell1.Text = f.FooterText;
var template = f as TemplateField;
if (template != null && template.FooterTemplate != null)
template.FooterTemplate.InstantiateIn(cell1);
footerRow.Cells.Add(cell1);
}
protected virtual void RenderEmptyContent(HtmlTextWriter writer)
{
Table t = new Table();
t.CssClass = this.CssClass;
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 headerRow = new TableRow();
t.Rows.Add(headerRow);
TableRow footerRow = new TableRow();
t.Rows.Add(footerRow);
foreach (DataControlField f in this.Columns)
{
TableHeaderCell cell = new TableHeaderCell();
cell.Text = f.HeaderText;
headerRow.Cells.Add(cell);
cell = new TableHeaderCell();
cell.Text = f.FooterText;
footerRow.Cells.Add(cell);
}
t.RenderControl(writer);
}