C# || How To Copy DataTable DataRow From One DataRow To Another Using C#
The following is a module with functions which demonstrates how to copy a DataTable DataRow from one DataRow to another using C#.
The function demonstrated on this page is an extension method, which copies all matching columns from the source DataRow to the destination DataRow. If no matching column exists between the two rows, the data at that column is skipped. This allows to safely copy a single row from one DataTable to other.
1. Copy DataRow
The example below demonstrates the use of ‘Utils.Collections.Extensions.CopyTo‘ to copy all matching columns from a source row to a destination row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
// Copy DataRow using Utils.Collections; // Declare the source datatable var table1 = new DataTable(); table1.Columns.Add("Name", typeof(string)); table1.Rows.Add("Kenneth"); table1.Rows.Add("Jennifer"); table1.Rows.Add("Lynn"); table1.Rows.Add("Sole"); table1.Rows.Add(System.DBNull.Value); // Declare the destination datatable var table2 = new DataTable(); table2.Columns.Add("Id", typeof(int)); table2.Columns.Add("Name", typeof(string)); for (var index = 0; index < table1.Rows.Count; ++index) { // Get the source row var rowSource = table1.Rows[index]; // Get the destination row var rowDestination = table2.NewRow(); // Do something with the destination row rowDestination["Id"] = index + 1; // Copy contents from source to destination rowSource.CopyTo(rowDestination); table2.Rows.Add(rowDestination); } // Display information from destination table foreach (DataRow row in table2.Rows) { Console.WriteLine($"Id: {row["Id"]}, Name: {row["Name"]}"); } // expected output: /* Id: 1, Name: Kenneth Id: 2, Name: Jennifer Id: 3, Name: Lynn Id: 4, Name: Sole Id: 5, Name: */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 13, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; using System.Data; namespace Utils { namespace Collections { public static class Extensions { /// <summary> /// Copies all matching columns from the source row to the destination row /// </summary> /// <param name="source">The DataRow source</param> /// <param name="destination">The DataRow destination</param> public static void CopyTo(this DataRow source, DataRow destination) { foreach (DataColumn col in source.Table.Columns) { if (destination.Table.Columns.Contains(col.ColumnName)) { destination[col.ColumnName] = source[col.ColumnName]; } } } } } } |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 13, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils.Collections; using System.Data; public class Program { static void Main(string[] args) { try { // Declare the source datatable var table1 = new DataTable(); table1.Columns.Add("Name", typeof(string)); table1.Rows.Add("Kenneth"); table1.Rows.Add("Jennifer"); table1.Rows.Add("Lynn"); table1.Rows.Add("Sole"); table1.Rows.Add(System.DBNull.Value); // Declare the destination datatable var table2 = new DataTable(); table2.Columns.Add("Id", typeof(int)); table2.Columns.Add("Name", typeof(string)); for (var index = 0; index < table1.Rows.Count; ++index) { // Get the source row var rowSource = table1.Rows[index]; // Get the destination row var rowDestination = table2.NewRow(); // Do something with the destination row rowDestination["Id"] = index + 1; // Copy contents from source to destination rowSource.CopyTo(rowDestination); table2.Rows.Add(rowDestination); } // Display information from destination table foreach (DataRow row in table2.Rows) { Display($"Id: {row["Id"]}, Name: {row["Name"]}"); } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply