Class DataTableExtensions
Extensions
public static class DataTableExtensions
- Inheritance
-
DataTableExtensions
- Inherited Members
Methods
AddNewRow(DataTable)
Creates a new row, adds the row to rows, and returns the row.
public static DataRow AddNewRow(this DataTable Table)
Parameters
TableDataTable
Returns
AppendTo(DataTable, DataTable)
Appends Source rows to Dest.
WARNING: Assumes that Source and Dest are identical in schema.
public static void AppendTo(this DataTable Source, DataTable Dest)
Parameters
ClearSchemaAndData(DataTable)
Clears all rows, constraints and columns from Target, leaving it with no schema and no data.
public static void ClearSchemaAndData(this DataTable Target)
Parameters
TargetDataTable
ContainsColumn(DataTable, string)
Returns true if Table contains a DataColumn with FieldName
public static bool ContainsColumn(this DataTable Table, string FieldName)
Parameters
Returns
CopyColumnsFrom(DataTable, DataTable)
Clears the columns of Target and re-creates them based on the columns of Source, copying the relevant column properties.
public static void CopyColumnsFrom(this DataTable Target, DataTable Source)
Parameters
CopyExactState(DataSet)
Copies tables from Source to Result.
For each table copies Source data rows to Result data rows, preserving the RowState.
public static DataSet CopyExactState(this DataSet Source)
Parameters
SourceDataSet
Returns
CopyExactState(DataSet, DataSet, bool)
Copies tables from Source to Dest.
For each table copies Source data rows to Dest data rows, preserving the RowState.
NOTE: It first clears any rows found in Dest tables.
If CopySchemaToo is true, it deletes Columns from Dest tables and creates new Columns based on Source tables
public static void CopyExactState(this DataSet Source, DataSet Dest, bool CopySchemaToo)
Parameters
CopyExactState(DataTable)
Copies Source data rows to Result data rows, preserving the RowState.
public static DataTable CopyExactState(this DataTable Source)
Parameters
SourceDataTable
Returns
CopyExactState(DataTable, DataTable, bool)
Copies Source data rows to Dest data rows, preserving the RowState.
NOTE: It first clears any rows found in Dest.
If CopySchemaToo is true, it deletes Columns from Dest and creates new Columns based on Source
public static void CopyExactState(this DataTable Source, DataTable Dest, bool CopySchemaToo)
Parameters
CopyRowsFrom(DataTable, DataTable)
Copies the rows of Source to Target, preserving the RowState of each row.
public static void CopyRowsFrom(this DataTable Target, DataTable Source)
Parameters
CopyStructure(DataTable)
Returns a new empty DataTable with a schema identical to Source.
WARNING: It preserves the class type of the Source. That is the result table is of the same class type as the Source.
public static DataTable CopyStructure(this DataTable Source)
Parameters
SourceDataTable
Returns
CopyStructure(DataTable, bool)
Returns a new empty DataTable with a schema identical to Source.
If PreserveClassType is true then the result table is of the same class type as the Source.
Else the result table is of the System.Data.DataTable type.
public static DataTable CopyStructure(this DataTable Source, bool PreserveClassType)
Parameters
Returns
CopyStructureAndRowsFrom(DataTable, DataTable)
Clears Target and copies the table name, namespace, locale, case-sensitivity, columns and rows from Source to Target.
public static void CopyStructureAndRowsFrom(this DataTable Target, DataTable Source)
Parameters
CopyStructureTo(DataTable, DataTable)
Copies the Source schema to Dest.
WARNING: Dest must be empty and no DataColumns defined.
public static void CopyStructureTo(this DataTable Source, DataTable Dest)
Parameters
CopyTo(DataTable, DataTable)
Copies Source rows to Dest.
WARNING: Dest is emptied first.
WARNING: Assumes that Source and Dest are identical in schema.
public static void CopyTo(this DataTable Source, DataTable Dest)
Parameters
CopyTo(DataTable, DataTable, bool)
Copies Source rows to Dest.
WARNING: Assumes that Source and Dest are identical in schema.
public static void CopyTo(this DataTable Source, DataTable Dest, bool EmptyDest)
Parameters
DeleteRows(DataTable)
A DataRow is marked as Deleted only if the DataRow.Delete() is called.
The DataRowCollection.Clear(), DataRowCollection.RemoveAt(), DataTable.Clear() etc, do NOT set the DataRowState.Deleted flag.
This method deletes all rows from a DataTable and sets the Deleted flag.
WARNING: Only DataRowState.Unchanged rows (which is set by a DataTable.AcceptChanges() etc) are marked as DataRowState.Deleted.
public static void DeleteRows(this DataTable Table)
Parameters
TableDataTable
FindColumn(DataTable, string)
Returns the DataColumn with FileName, if exists, else null.
public static DataColumn FindColumn(this DataTable Table, string FieldName)
Parameters
Returns
GetColumn(DataTable, string)
Returns the DataColumn with FileName, if exists, else exception.
public static DataColumn GetColumn(this DataTable Table, string FieldName)
Parameters
Returns
GetDeletedRows(DataTable)
Returns a DataTable with the deleted rows in the Source.
A DataRow is marked with the Deleted flag when it is deleted. After that is not possible to access the row data without an exception.
By getting deleted rows of a table to another table, eliminates this problem.
public static DataTable GetDeletedRows(this DataTable Source)
Parameters
SourceDataTable
Returns
GetKeyValuesList(DataTable, string, int)
Used in constructing SQL statements that contain a WHERE clause of the type
where FIELD_NAME in (...)
This method limits the number of elements inside the in (...) according to the passed in ModValue, in order to avoid problems with database servers that have such a limit.
It returns a string array where each element contains no more than ModValue of the FieldName values from Table.
public static List<string> GetKeyValuesList(this DataTable Table, string FieldName, int ModValue = 100)
Parameters
Returns
GetRowCount(DataTable)
Returns the REAL number of rows in Table, not counting DataRowState.Deleted rows
public static int GetRowCount(this DataTable Table)
Parameters
TableDataTable
Returns
IndexOfColumn(DataTable, string)
Returns the index of the DataColumn with FieldName in the Table, if any, else -1.
public static int IndexOfColumn(this DataTable Table, string FieldName)
Parameters
Returns
IsStringField(DataTable, string)
Returns true if FieldName is of type string.
public static bool IsStringField(this DataTable Table, string FieldName)
Parameters
Returns
Locate(DataTable, string, object, LocateOptions)
Returns the first row in Table whose value for FieldName matches Value, according to Options, if found, else null.
public static DataRow Locate(this DataTable Table, string FieldName, object Value, LocateOptions Options)
Parameters
TableDataTableFieldNamestringValueobjectOptionsLocateOptions
Returns
Locate(DataTable, string[], object[], LocateOptions)
Returns the first row in Table whose values for FieldNames match Values, according to Options, if found, else null.
public static DataRow Locate(this DataTable Table, string[] FieldNames, object[] Values, LocateOptions Options)
Parameters
TableDataTableFieldNamesstring[]Valuesobject[]OptionsLocateOptions
Returns
MergeStructure(DataTable, DataTable)
Copies column schema from Source to Dest. Only DataColumns that do not exist in Dest are copied.
public static void MergeStructure(this DataTable Source, DataTable Dest)
Parameters
SafeAppendTo(DataTable, DataTable)
Appends Source rows to Dest.
WARNING: Only data from columns with identical names to both tables are copied.
public static void SafeAppendTo(this DataTable Source, DataTable Dest)
Parameters
SafeCopyTo(DataTable, DataTable)
Copies Source rows to Dest.
WARNING: Dest is emptied first.
WARNING: Only data from columns with identical names to both tables are copied.
public static void SafeCopyTo(this DataTable Source, DataTable Dest)
Parameters
SafeCopyTo(DataTable, DataTable, bool)
Copies Source rows to Dest.
WARNING: Only data from columns with identical names to both tables are copied.
public static void SafeCopyTo(this DataTable Source, DataTable Dest, bool EmptyDest)
Parameters
SetColumnCaptionsFrom(DataTable, IDictionary<string, string>, bool)
Sets Table column captions. Dictionary is a ColumnName=Caption list of pairs. If HideUntitle is true, then any column not found in Dictionary is set to Visible = false in its ExtendedProperties.
public static void SetColumnCaptionsFrom(this DataTable Table, IDictionary<string, string> Dictionary, bool HideUntitled)
Parameters
TableDataTableDictionaryIDictionary<string, string>HideUntitledbool
SetColumnsVisible(DataTable, bool)
Sets the Visible "extended property" of all Table.Columns to Value.
public static void SetColumnsVisible(this DataTable Table, bool Value)
Parameters
SplitToChunks(DataTable, int)
Splits a specified table's rows into chunks. Each chunk may have a specified row count.
public static DataRow[][] SplitToChunks(this DataTable Table, int ChunkRowCount)
Parameters
Returns
- DataRow[][]
ToObservableCollection(DataTable)
Converts the rows of a DataTable to an observable collection.
public static ObservableCollection<DataRow> ToObservableCollection(this DataTable Table)
Parameters
TableDataTable
Returns
ToTable(IEnumerable<DataRow>)
Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable of DataRow.
.NetStandard 2.1 contains the extension method CopyToDataTable() with the same functionality.
public static DataTable ToTable(this IEnumerable<DataRow> Rows)
Parameters
RowsIEnumerable<DataRow>