Exchanging the row and column positions in a able
For example, the data we retrieve from the database is placed in the able as follows:
Title |
Column 1 |
Column 2 |
Column 3 |
Row 1 |
34 |
23 |
65 |
Row 2 |
67 |
56 |
89 |
The data obtained after row-column exchange is:
Title |
Row 1 |
Row 2 |
Column 1 |
34 |
67 |
Column 2 |
23 |
56 |
Column 3 |
65 |
89 |
The following is an exchange function. The Code is as follows:
Private datatable swapdtcr (datatable inputdt) {datatable outputdt = new datatable (); // The Position of the title remains unchanged outputdt. columns. add (inputdt. columns [0]. columnname. tostring (); foreach (datarow inrow in inputdt. rows) {string newcolname = inrow [0]. tostring (); outputdt. columns. add (newcolname) ;}for (INT rcount = 1; rcount <= inputdt. columns. count-1; rcount ++) {datarow newrow = outputdt. newrow (); newrow [0] = inputdt. columns [rcount]. columnname. tostring (); For (INT ccount = 0; ccount <= inputdt. rows. count-1; ccount ++) {string colvalue = inputdt. rows [ccount] [rcount]. tostring (); newrow [ccount + 1] = colvalue;} outputdt. rows. add (newrow);} return outputdt ;}