public Gridview DataBind <T>(List <T> dataList) { if (this.Columns.Count == 0) { return(this); } if (this.Columns.Count == 0 || dataList == null || dataList.Count == 0) { return(this); } GridviewRow newRow = null; GridviewRowCell newCell = null; int rowIndex = -1; foreach (object listItem in dataList) { newRow = new GridviewRow(); rowIndex++; foreach (GridviewColumn column in this.Columns) { newCell = new GridviewRowCell(); if (string.IsNullOrWhiteSpace(column.Value)) { newCell.Caption = string.Empty; } else { var caption = GetCaption(listItem, column.Value); newCell.Caption = caption != null?caption.ToString() : string.Empty; } if (this.CellPreparing != null) { var eventArgs_CellPreparing = new CellPreparingEventArgs(column, newCell, listItem, rowIndex); this.CellPreparing(this, eventArgs_CellPreparing); } newRow.AddRowCells(newCell); } if (this.RowPreparing != null) { var eventArgs_RowPreparing = new RowPreparingEventArgs(newRow, listItem); this.RowPreparing(this, eventArgs_RowPreparing); } this.AddRows(newRow); } return(this); }
public Gridview DataBind(DataTable dataTable) { if (this.Columns.Count == 0) { return(this); } if (this.Columns.Count == 0 || dataTable == null || dataTable.Rows.Count == 0) { return(this); } GridviewRow newRow = null; GridviewRowCell newCell = null; int rowIndex = -1; foreach (DataRow dataRow in dataTable.Rows) { newRow = new GridviewRow(); rowIndex++; foreach (GridviewColumn column in this.Columns) { newCell = new GridviewRowCell(); if (string.IsNullOrWhiteSpace(column.Value)) { newCell.Caption = string.Empty; } else { newCell.Caption = dataRow[column.Value].ToString(); } if (this.CellPreparing != null) { var eventArgs_CellPreparing = new CellPreparingEventArgs(column, newCell, dataRow, rowIndex); this.CellPreparing(this, eventArgs_CellPreparing); } newRow.AddRowCells(newCell); } if (this.RowPreparing != null) { var eventArgs_RowPreparing = new RowPreparingEventArgs(newRow, dataRow); this.RowPreparing(this, eventArgs_RowPreparing); } this.AddRows(newRow); } return(this); }