Developer404
Developer404

Reputation: 5972

Simple way to convert datarow array to datatable

I want to convert a DataRow array into DataTable ... What is the simplest way to do this?

Upvotes: 112

Views: 404245

Answers (15)

BehrouzMoslem
BehrouzMoslem

Reputation: 9713

  public static void AddRange(this DataColumnCollection @this, params string[] columns)
        {
            foreach (string column in columns)
            {
                @this.Add(column);
            }
        }
        public static DataTable ConvertEnumerableRowCollectionToDataTable(EnumerableRowCollection<DataRow> drs, string[] columns)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(columns);
            foreach (DataRow dr in drs)
            {

                dt.ImportRow(dr);
            }

            return dt;
        }

Upvotes: 0

Awais Shabir
Awais Shabir

Reputation: 43

You need to clone the structure of Data table first then import rows using for loop

DataTable dataTable =dtExisting.Clone();
foreach (DataRow row in rowArray) {
   dataTable.ImportRow(row);
}

Upvotes: 1

Rajenthiran T
Rajenthiran T

Reputation: 73

DataTable Assetdaterow =
    (
        from s in dtResourceTable.AsEnumerable()
        where s.Field<DateTime>("Date") == Convert.ToDateTime(AssetDate)
        select s
    ).CopyToDataTable();

Upvotes: 6

AVINASH DUBEY
AVINASH DUBEY

Reputation: 1

DataTable dataTable = new DataTable();
dataTable = OldDataTable.Tables[0].Clone();
foreach(DataRow dr in RowData.Tables[0].Rows)
{
 DataRow AddNewRow = dataTable.AddNewRow();
 AddNewRow.ItemArray = dr.ItemArray;
 dataTable.Rows.Add(AddNewRow);
}

Upvotes: 0

user1036202
user1036202

Reputation: 51

DataTable dt = new DataTable();
foreach (DataRow dr in drResults)
{ 
    dt.ImportRow(dr);
}   

Upvotes: 5

Andr&#233;s Silva
Andr&#233;s Silva

Reputation: 39

You could use System.Linq like this:

if (dataRows != null && dataRows.Length > 0)
{
   dataTable = dataRows.AsEnumerable().CopyToDataTable();
}

Upvotes: 3

Haseeb Mukhtar
Haseeb Mukhtar

Reputation: 158

.Net 3.5+ added DataTableExtensions, use DataTableExtensions.CopyToDataTable Method

For datarow array just use .CopyToDataTable() and it will return datatable.

For single datarow use

new DataRow[] { myDataRow }.CopyToDataTable()

Upvotes: 4

logixologist
logixologist

Reputation: 3834

Incase anyone needs it in VB.NET:

Dim dataRow as DataRow
Dim yourNewDataTable as new datatable
For Each dataRow In yourArray
     yourNewDataTable.ImportRow(dataRow)
Next

Upvotes: 1

Zolfaghari
Zolfaghari

Reputation: 1323

Simple way is:

// dtData is DataTable that contain data
DataTable dt = dtData.Select("Condition=1").CopyToDataTable();

// or existing typed DataTable dt
dt.Merge(dtData.Select("Condition=1").CopyToDataTable());

Upvotes: 11

joe
joe

Reputation: 2462

For .Net Framework 3.5+

DataTable dt = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt1 = dr.CopyToDataTable();

But if there is no rows in the array, it can cause the errors such as The source contains no DataRows. Therefore, if you decide to use this method CopyToDataTable(), you should check the array to know it has datarows or not.

if (dr.Length > 0)
    DataTable dt1 = dr.CopyToDataTable();

Reference available at MSDN: DataTableExtensions.CopyToDataTable Method (IEnumerable)

Upvotes: 218

Miriam
Miriam

Reputation: 59

DataTable dt = myDataRowCollection.CopyToDataTable<DataRow>();

Upvotes: 5

ilans
ilans

Reputation: 2727

Another way is to use a DataView

// Create a DataTable
DataTable table = new DataTable()
...

// Filter and Sort expressions
string expression = "[Birth Year] >= 1983"; 
string sortOrder = "[Birth Year] ASC";

// Create a DataView using the table as its source and the filter and sort expressions
DataView dv = new DataView(table, expression, sortOrder, DataViewRowState.CurrentRows);

// Convert the DataView to a DataTable
DataTable new_table = dv.ToTable("NewTableName");

Upvotes: 11

zaib shah
zaib shah

Reputation: 21

Here is the solution. It should work fine.

DataTable dt = new DataTable();
dt = dsData.Tables[0].Clone();
DataRows[] drResults = dsData.Tables[0].Select("ColName = 'criteria');

foreach(DataRow dr in drResults)
{
    object[] row = dr.ItemArray;
    dt.Rows.Add(row);
} 

Upvotes: 2

Jay Riggs
Jay Riggs

Reputation: 53603

Why not iterate through your DataRow array and add (using DataRow.ImportRow, if necessary, to get a copy of the DataRow), something like:

foreach (DataRow row in rowArray) {
   dataTable.ImportRow(row);
}

Make sure your dataTable has the same schema as the DataRows in your DataRow array.

Upvotes: 98

Mitch Wheat
Mitch Wheat

Reputation: 300769

DataTable dt = new DataTable(); 

DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria");

dt.Rows.Add(dr);

Upvotes: 12

Related Questions