Sam Beauvois: general dev, .net and other stuff

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

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];

}

}

}

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

You can follow any responses to this entry through the RSS 2.0 feed.