public class DataTableHelper { ///Calling The Function/// To Create Table Skeleton. /// /// Table Column Name as an Array/// Column DataType as an Array/// Name of the DataTable///public DataTable CreateDataTable(string[] ColumnName, string[] DataType, string TableName) { // Create DataTable. DataTable table = new DataTable(TableName); try { for (int i = 0; i < ColumnName.Length; i++) { // Create DataColumns and add them to DataTable. table.Columns.Add(CreateDataColumn(DataType[i].Trim(), ColumnName[i].Trim(), "Column", false, false, false)); } } catch (Exception) { } return table; } /// /// To create the table structure /// /// Column DataType/// ColumnName/// Column Caption/// Is Auto Increament/// IsReadOnly/// ///private DataColumn CreateDataColumn(string colType, string name, string caption, bool autoInc, bool readOnly, bool unique) { DataColumn column = new DataColumn(); try { column.DataType = System.Type.GetType(colType); column.ColumnName = name; column.Caption = caption; column.AutoIncrement = autoInc; column.ReadOnly = readOnly; column.Unique = unique; } catch (Exception) { } return column; } /// /// Add Row to a DataTable /// //How to Call? /// //tempDt = oblDataTableHelper.AddRowsToTable(strArrField, tempDt); /// /// Column Data as String Array/// Name of DataTable to add the Row///public DataTable AddRowsToTable(string[] value, DataTable table) { // Generate row with values for each column. DataRow row = table.NewRow(); try { //Add values to the columns for (int i = 0; i < value.Length; i++) row[i] = value[i]; // Add row to table. table.Rows.Add(row); } catch (Exception) { } return table; } /// /// Check whether the table contains Duplicate value for a particular column /// /// DataTable/// Name of Column to Check for Duplicate///public bool HasDuplicates(DataTable table, string columnName) { bool IsContainsDuplicates = false; try { Hashtable hTable = new Hashtable(); foreach (DataRow row in table.Rows) if (!hTable.Contains(row[columnName])) hTable.Add(row[columnName], null); else { IsContainsDuplicates = true; break; } } catch (Exception) { } return IsContainsDuplicates; } /// /// Remove rows for certains columns contains duplicate value /// //How to Call? /// tempDt = oblDataTableHelper.RemoveDuplicateRows(tempDt, new System.Collections.Generic.List /// DataTable Instance/// ColumnName as List(ColumnName)); /// /// public DataTable RemoveDuplicateRows(DataTable table, List keyColumns) { //DataTable results = myDataTable.AsEnumerable().Distinct().CopyToDataTable(); DataTable duplicateTable = table.Clone(); DataColumn[] primaryKey = new DataColumn[duplicateTable.Columns.Count]; duplicateTable.Columns.CopyTo(primaryKey, 0); duplicateTable.PrimaryKey = primaryKey; DataRow[] dataRows = new DataRow[table.Rows.Count]; table.Rows.CopyTo(dataRows, 0); foreach (DataRow dataRow in dataRows) if (duplicateTable.Rows.Contains(dataRow.ItemArray)) table.Rows.Remove(dataRow); else duplicateTable.Rows.Add(dataRow.ItemArray); return table; } //tempDt = oblDataTableHelper.RemoveDuplicateRows(tempDt, ColumnName); /// /// Remove rows for certains columns contains duplicate value /// /// DataTable Instance/// ColumnName as Array///public DataTable RemoveDuplicateRows(DataTable dTable, string[] strColumns) { // create a dv from the source dt DataView dv = new DataView(dTable); // set the output columns array of the destination dt // true = yes, i need distinct values. return dv.ToTable(true, strColumns); } }
DataTableHelper oblDataTableHelper = new DataTableHelper(); string[] ColumnName = new string[] { "Column1", "Column2", "Column3", "Column4" }; string[] DataType = new string[] { "System.String", "System.String", "System.String", "System.String" }; System.Data.DataTable tempDt = null; tempDt = oblDataTableHelper.CreateDataTable(ColumnName, DataType, "DataTableName");Path that gives me the idea