1

Copy datatable’s row to an other datatable’s row

Posted January 16th, 2010 in .NET by Sam Beauvois

This method perform a row copy with a few checks


public static void CopyRowToAnOther(DataRow row, DataRow anOtherRow)
        {

            if (row == null || row.Table == null
                || anOtherRow == null || anOtherRow.Table == null)
            {
                return;
            }

            foreach (DataColumn colunm in row.Table.Columns)
            {
                if (anOtherRow.Table.Columns.Contains(colunm.ColumnName)
                && !row.IsNull(colunm.ColumnName)
                && colunm.DataType == anOtherRow.Table.Columns[colunm.ColumnName].DataType)
                {
                    anOtherRow[colunm.ColumnName] = row[colunm.ColumnName];
                }
            }
        }
public static void CopyRowToAnOther(DataRow row, DataRow anOtherRow)

{

if (row == null || row.Table == null || anOtherRow == null || anOtherRow.Table==null)

{

return;

}

foreach (DataColumn colunm in row.Table.Columns)

{

if (anOtherRow.Table.Columns.Contains(colunm.ColumnName)

&& !row.IsNull(colunm.ColumnName)

&& colunm.DataType == anOtherRow.Table.Columns[colunm.ColumnName].DataType)

{

anOtherRow[colunm.ColumnName] = row[colunm.ColumnName];

}

}

}

One Response so far.

  1. nice read. I would love to follow you on twitter.

Leave a Reply