1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| [ToolboxData("<{0}:GridViewWithAlwaysShowFooter runat=server></{0}:GridViewWithAlwaysShowFooter>")]
public class GridViewWithAlwaysShowFooter : GridView
{
private GridViewRow _headerRow;
private GridViewRow _footerRow;
private bool _showFooterWhenEmpty;
[Category("Behavior")]
[Themeable(true)]
[Bindable(BindableSupport.No)]
public bool ShowFooterWhenEmpty
{
get { return _showFooterWhenEmpty; }
set { _showFooterWhenEmpty = value; }
}
public override GridViewRow HeaderRow
{
get { return base.HeaderRow ?? _headerRow; }
}
public override GridViewRow FooterRow
{
get { return base.FooterRow ?? _footerRow; }
}
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int rows = base.CreateChildControls(dataSource, dataBinding);
if (rows == 0 && (ShowFooterWhenEmpty || ShowHeaderWhenEmpty))
{
// create the table
Table table = CreateChildTable();
Controls.Clear();
Controls.Add(table);
DataControlField[] fields;
if (AutoGenerateColumns)
{
PagedDataSource source = new PagedDataSource();
source.DataSource = dataSource;
ICollection autoGeneratedColumns = CreateColumns(source, true);
fields = new DataControlField[autoGeneratedColumns.Count];
autoGeneratedColumns.CopyTo(fields, 0);
}
else
{
fields = new DataControlField[Columns.Count];
Columns.CopyTo(fields, 0);
}
TableRowCollection newRows = table.Rows;
if (ShowHeaderWhenEmpty)
{
// create a new header row
_headerRow = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
InitializeRow(_headerRow, fields, newRows);
}
// create the empty row
GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = fields.Length;
cell.Width = Unit.Percentage(100);
// respect the precedence order if both EmptyDataTemplate
// and EmptyDataText are both supplied ...
if (EmptyDataTemplate != null)
{
EmptyDataTemplate.InstantiateIn(cell);
}
else if (!string.IsNullOrEmpty(EmptyDataText))
{
cell.Controls.Add(new LiteralControl(EmptyDataText));
}
emptyRow.Cells.Add(cell);
GridViewRowEventArgs e = new GridViewRowEventArgs(emptyRow);
OnRowCreated(e);
newRows.Add(emptyRow);
emptyRow.DataBind();
OnRowDataBound(e);
emptyRow.DataItem = null;
if (ShowFooterWhenEmpty)
{
// create footer row
_footerRow = CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
InitializeRow(_footerRow, fields, newRows);
}
}
return rows;
}
private void InitializeRow(GridViewRow row, DataControlField[] fields, TableRowCollection newRows)
{
GridViewRowEventArgs e = new GridViewRowEventArgs(row);
InitializeRow(row, fields);
OnRowCreated(e);
newRows.Add(row);
row.DataBind();
OnRowDataBound(e);
row.DataItem = null;
}
} |
Partager