How to exchange an entire row of one DataTable for another?

3

I have a Datatable with 4 columns in which there are 5 rows filled so that in total I have 20 filled cells.

How can I then change the data of the 4 cells in row 2 to row 4 and from row 4 to row 2?

DataTable inicial:

   c1  c2  c3  c4
f1 d11 d12 d13 d14
f2 d21 d22 d23 d24
f3 d31 d32 d33 d34
f4 d41 d42 d43 d44
f5 d51 d52 d53 d54


DataTable final:

   c1  c2  c3  c4
f1 d11 d12 d13 d14
f2 d41 d42 d43 d44
f3 d31 d32 d33 d34
f4 d21 d22 d23 d24
f5 d51 d52 d53 d54
    
asked by Popularfan 21.08.2018 в 17:38
source

1 answer

2

We are going to modify the property ItemArray in this way we manipulate at the value level.

   DataTable dt = new DataTable();
  //Guardamos fila 2
   object[] row2 = dt.Rows[1].ItemArray;
 //Insertamos en la fila 2 ,el valor de la fila 4
  dt.Rows[1].ItemArray = dt.Rows[3].ItemArray;
  //Insertamos en la fila 2 ,el valor que guardamos de la fila 2
  dt.Rows[3].ItemArray = row2;
    
answered by 21.08.2018 / 17:46
source