Reprinted csdn (educast): C # merge the two datatables with the same structure

Source: Internet
Author: User

Datatable DB = NULL;
Datatable dt = Ds. Tables [0];
//// Merge two data tables
DB = DT. Copy ();
Datarow [] foundrows = DT. Select ();
For (INT I = 0; I <foundrows. length; I ++)
{
DB. importrow (foundrows [I]);
}

  1. // Merge DT with the same structure
  2. Datatable ableable1 = new datatable ();
  3. Datatable ableable2 = new datatable ();
  4. Datatable newdatatable = datatable1.clone ();
  5. Object [] OBJ = new object [newdatatable. Columns. Count];
  6. For (INT I = 0; I <ableable1.rows. Count; I ++)
  7. {
  8. Datatable1.rows [I]. itemarray. copyto (OBJ, 0 );
  9. Newdatatable. Rows. Add (OBJ );
  10. }
  11. For (INT I = 0; I <ableable2.rows. Count; I ++)
  12. {
  13. Datatable2.rows [I]. itemarray. copyto (OBJ, 0 );
  14. Newdatatable. Rows. Add (OBJ );
  15. }
  16. // Merge DT with different structures
  17. /// <Summary>
  18. /// Combine two different able columns into a new datatable
  19. /// </Summary>
  20. /// <Param name = "dt1"> Table 1 </param>
  21. /// <Param name = "dt2"> Table 2 </param>
  22. /// <Param name = "dtname"> name of the merged table </param>
  23. /// <Returns> </returns>
  24. Private datatable unitedatatable (datatable dt1, datatable dt2, string dtname)
  25. {
  26. Datatable dt3 = dt1.clone ();
  27. For (INT I = 0; I <dt2.columns. Count; I ++)
  28. {
  29. Dt3.columns. Add (dt2.columns [I]. columnname );
  30. }
  31. Object [] OBJ = new object [dt3.columns. Count];
  32. For (INT I = 0; I <dt1.rows. Count; I ++)
  33. {
  34. Dt1.rows [I]. itemarray. copyto (OBJ, 0 );
  35. Dt3.rows. Add (OBJ );
  36. }
  37. If (dt1.rows. Count> = dt2.rows. Count)
  38. {
  39. For (INT I = 0; I <dt2.rows. Count; I ++)
  40. {
  41. For (Int J = 0; j <dt2.columns. Count; j ++)
  42. {
  43. Dt3.rows [I] [J + dt1.columns. Count] = dt2.rows [I] [J]. tostring ();
  44. }
  45. }
  46. }
  47. Else
  48. {
  49. Datarow dr3;
  50. For (INT I = 0; I <dt2.rows. Count-dt1.rows. Count; I ++)
  51. {
  52. Dr3 = dt3.newrow ();
  53. Dt3.rows. Add (dr3 );
  54. }
  55. For (INT I = 0; I <dt2.rows. Count; I ++)
  56. {
  57. For (Int J = 0; j <dt2.columns. Count; j ++)
  58. {
  59. Dt3.rows [I] [J + dt1.columns. Count] = dt2.rows [I] [J]. tostring ();
  60. }
  61. }
  62. }
  63. Dt3.tablename = dtname; // sets the DT name.
  64. Return dt3;
  65. }
  66. // Merge multiple data tables with the same structure
  67. Public datatable getallentrysdatatable ()
  68. {
  69. Datatable newdatatable = getentrysdatatable (0). Clone ();
  70. Object [] OBJ = new object [newdatatable. Columns. Count];
  71. For (INT I = 0; I <entrygroups. getentrygroupcount (); I ++)
  72. {
  73. For (Int J = 0; j <getentrysdatatable (I). Rows. Count; j ++)
  74. {
  75. Getentrysdatatable (I). Rows [J]. itemarray. copyto (OBJ, 0 );
  76. Newdatatable. Rows. Add (OBJ );
  77. }
  78. }
  79. Return newdatatable;
  80. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.