Copy datatable’s row to another 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];

}

}

}

Incoming search terms:

  • copytodatatable

About Sam Beauvois

Application Developer, .NET enthusiast since 2004, I'm interested in technology watch, usability, code quality, patterns & practices, UX, ...

4 comments

  1. admin says:

    good point! Fixed ;)

  2. Just a picky little thing but… “another” is a single word, not two.
    Reading “AnOther” and “anOther” looks awkward.
    Thanks

  3. Jamshed says:

    Some cool invinatove designs. They all look stunning!!Keep up the good work cheers!!

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>