Exchanging the row and column positions in a able

Source: Internet
Author: User
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 ;}

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.