How to swap two rows in a datatable



Is there a way to swap two rows in a datatable, and export it after ?

Thank you


Yes @Obee

You can swap two rows in a datatable with below code. (one way i have done this like below)

DataRow selected_first_row_to_swap= YourDatatable.Rows[index_of_that_row]; // YourDatatable.Rows(0)
DataRow selected_second_row_to_swap= YourDatatable.Rows[index_of_that_row]; // YourDatatable.Rows(2)
DataRow newRow = YourDatatable.NewRow()
DataRow newRow1 = YourDatatable.NewRow()
newRow.ItemArray = selected_first_row_to_swap.ItemArray; // copying data
newRow1.ItemArray = selected_second_row_to_swap.ItemArray; // copying data 
YourDatatable.Rows.InsertAt(newRow, index_where_you_wants_to_insert); // index 2
YourDatatable.Rows.InsertAt(newRow1, index_where_you_wants_to_insert);  // index 0

For your reference here a sample workflow, in below sample workflow i am swaping datarow 0 with datarow 2 ( i.e. first row with third row).

swap_datarows.xaml (14.0 KB)


Adding a row to a data table at a specified index