SpreadsheetGear The Performance Spreadsheet Component Company 
CopyFromDataTable Method
See Also 
SpreadsheetGear Namespace > IRange Interface : CopyFromDataTable Method
dataTable
Specifies the source DataTable.
flags
Specifies options including All Text values, No Column Headers and Insert Cells.
Copies the specified DataTable to this range.

Syntax

C# 
virtual void CopyFromDataTable( 
   DataTable dataTable,
   SetDataFlags flags
)

Parameters

dataTable
Specifies the source DataTable.
flags
Specifies options including All Text values, No Column Headers and Insert Cells.

Example

See DataSet to Excel Workbook with Formats and Formulas in the Excel Reporting Samples and DataTable to Excel Workbook with Formats and Formulas in the Excel to DataGrid Samples for examples of using CopyFromDataTable.

Remarks

If this range is a single column, the destination range will be expanded to the number of columns in the DataTable.

If this range is a single row, the destination will be expanded to the number of rows in the DataTable, plus one row for the column headers if needed.

Note that you must specify a single data row and allow the destination size to be determined automatically if you use WrapToNewWorksheet to enable wrapping rows to the next worksheet if they do not fit on the current worksheet.

Using Formats and Formulas
To preformat a range and have formulas refer to columns in the range, specify the SpreadsheetGear.Data.SetDataFlags.InsertCells flag. When specifying this flag, you must provide a range with exactly two rows for data, preceded by a row for column headers if SpreadsheetGear.Data.SetDataFlags.NoColumnNames is not specified.

Specifying this flag instructs SpreadsheetGear for .NET to insert or delete rows as needed to accommodate the number of rows in the source DataTable, and to copy down values or formulas in extra columns which enables "calculated columns". This allows the cells to be preformatted and allows formulas to reference cells within the destination range. If the DataTable contains no rows, both data rows will be deleted and any formula references to these cells will be changed to #REF!. Otherwise, formulas referring to cells in the second initial row will be fixed up to refer to the last row of data inserted into the worksheet.

DataTable Column Names to Range Column Headers
By default, column names are copied to the first row in the specified range. To disable this functionality, specify SpreadsheetGear.Data.DataSet.NoColumnHeaders.

Forcing Some or All Values to Text
Normally, an attempt is made to parse values as they are placed into cells. To force all values to be entered into cells as text, specify the SpreadsheetGear.Data.SetDataFlags.AllText flag. To force only specific columns to be placed into cells as text, use the SpreadsheetGear.Data.SetDataFlags.InsertCells flag which enables you to preformat these columns as text cells (IRange.NumberFormat = "@").

Requirements

Platforms: Windows Vista, Windows XP, Windows Server 2008, Windows Server 2003, Windows 2000, Windows Me and Windows 98, including 32 bit and 64 bit editions where applicable. SpreadsheetGear for .NET 1.x requires the Microsoft .NET Framework 1.1 or .NET 2.0 (works with .NET 3.x). SpreadsheetGear for .NET 2007 requires the Microsoft .NET Framework 2.0 (works with .NET 3.x).

See Also