private void InitializeRow(GridViewRow row, DataControlField[] fields, TableRowCollection newRows) { var e = new GridViewRowEventArgs(row); InitializeRow(row, fields); OnRowCreated(e); newRows.Add(row); row.DataBind(); OnRowDataBound(e); row.DataItem = null; }
protected override int CreateChildControls (IEnumerable data, bool dataBinding) { // clear GridView Controls.Clear (); table = null; rows = null; if (data == null) { return 0; } PagedDataSource dataSource; if (dataBinding) { DataSourceView view = GetData (); dataSource = new PagedDataSource (); dataSource.DataSource = data; if (AllowPaging) { dataSource.AllowPaging = true; dataSource.PageSize = PageSize; if (view.CanPage) { dataSource.AllowServerPaging = true; if (SelectArguments.RetrieveTotalRowCount) dataSource.VirtualCount = SelectArguments.TotalRowCount; } if (PageIndex >= dataSource.PageCount) pageIndex = dataSource.PageCount - 1; dataSource.CurrentPageIndex = PageIndex; } PageCount = dataSource.PageCount; } else { dataSource = new PagedDataSource (); dataSource.DataSource = data; if (AllowPaging) { dataSource.AllowPaging = true; dataSource.PageSize = PageSize; dataSource.CurrentPageIndex = PageIndex; } } bool createPager = AllowPaging && (PageCount >= 1) && PagerSettings.Visible; ArrayList list = new ArrayList (); // Creates the set of fields to show _dataEnumerator = null; ICollection fieldCollection = CreateColumns (dataSource, dataBinding); int fieldCount = fieldCollection.Count; DataControlField dcf; DataControlField[] fields = new DataControlField [fieldCount]; fieldCollection.CopyTo (fields, 0); for (int i = 0; i < fieldCount; i++) { dcf = fields [i]; dcf.Initialize (AllowSorting, this); if (EnableSortingAndPagingCallbacks) dcf.ValidateSupportsCallback (); } bool skip_first = false; IEnumerator enumerator; if (_dataEnumerator != null) { // replaced when creating bound columns enumerator = _dataEnumerator; skip_first = true; } else { enumerator = dataSource.GetEnumerator (); } // Main table creation while (skip_first || enumerator.MoveNext ()) { skip_first = false; object obj = enumerator.Current; if (list.Count == 0) { if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) { topPagerRow = CreatePagerRow (fieldCount, dataSource); OnRowCreated (new GridViewRowEventArgs (topPagerRow)); ContainedTable.Rows.Add (topPagerRow); if (dataBinding) { topPagerRow.DataBind (); OnRowDataBound (new GridViewRowEventArgs (topPagerRow)); } if (PageCount == 1) topPagerRow.Visible = false; } GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal); InitializeRow (headerRow, fields); OnRowCreated (new GridViewRowEventArgs (headerRow)); ContainedTable.Rows.Add (headerRow); if (dataBinding) { headerRow.DataBind (); OnRowDataBound (new GridViewRowEventArgs (headerRow)); } } DataControlRowState rstate = GetRowState (list.Count); GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate); row.DataItem = obj; list.Add (row); InitializeRow (row, fields); OnRowCreated (new GridViewRowEventArgs (row)); ContainedTable.Rows.Add (row); if (dataBinding) { row.DataBind (); if (EditIndex == row.RowIndex) oldEditValues = new DataKey (GetRowValues (row, true, true)); DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames)); OnRowDataBound (new GridViewRowEventArgs (row)); } } if (list.Count == 0) { GridViewRow emptyRow = CreateEmptyrRow (fieldCount); if (emptyRow != null) { OnRowCreated (new GridViewRowEventArgs (emptyRow)); ContainedTable.Rows.Add (emptyRow); if (dataBinding) { emptyRow.DataBind (); OnRowDataBound (new GridViewRowEventArgs (emptyRow)); } } return 0; } else { GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal); InitializeRow (footerRow, fields); OnRowCreated (new GridViewRowEventArgs (footerRow)); ContainedTable.Rows.Add (footerRow); if (dataBinding) { footerRow.DataBind (); OnRowDataBound (new GridViewRowEventArgs (footerRow)); } if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) { bottomPagerRow = CreatePagerRow (fieldCount, dataSource); OnRowCreated (new GridViewRowEventArgs (bottomPagerRow)); ContainedTable.Rows.Add (bottomPagerRow); if (dataBinding) { bottomPagerRow.DataBind (); OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow)); } if (PageCount == 1) bottomPagerRow.Visible = false; } } rows = new GridViewRowCollection (list); if (!dataBinding) return -1; if (AllowPaging) return dataSource.DataSourceCount; else return list.Count; }
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) { int rows = base.CreateChildControls(dataSource, dataBinding); // no data rows created, create empty table if enabled if (rows == 0 && (ShowFooterWhenEmpty || ShowHeaderWhenEmpty)) { // create the table Table table = CreateChildTable(); Controls.Clear(); Controls.Add(table); DataControlField[] fields; if (AutoGenerateColumns) { var source = new PagedDataSource { 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(); List<DataControlField> f = fields.Where(dataControlField => dataControlField.Visible).ToList(); cell.ColumnSpan = f.Count; //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 && ShowFooter) { // create footer row _footerRow = CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal); InitializeRow(_footerRow, fields, newRows); newRows.Remove(emptyRow); } } return rows; }