Merge DataTables in C#

Friday, July 19, 2013

Merge DataTables in C#









        /// <summary>
        /// This will merge all data in an array datatable
        /// </summary>
        /// <param name="dt">Array DataTable.</param>
        /// <returns>Merge DataTable.</returns>
        public static DataTable MergeDataTable(DataTable[] dt)
        {
            DataTable dt2 = new DataTable();

            for (int i = 0; i < dt.Length; i++)
            {
                foreach (DataColumn column in dt[i].Columns)
                {
                    if (!dt2.Columns.Contains(column.ColumnName))
                    {
                        dt2.Columns.Add(column.ColumnName, column.DataType);
                    }
                }
                foreach (DataRow item in dt[i].Rows)
                {
                    if (!dt2.Rows.Equals(item))
                    {
                        dt2.ImportRow(item);
                    }
                }
            }
            return dt2;
        }


2 comments :



  1. iFind Data Recovery Enterprise Crack>I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me

    ReplyDelete
  2. Please continue sharing your knowledge to help other students want to learn about programming. I hope will you continuing build a project like this. thank you for all knowledge you share.

    ReplyDelete