In this walk-through, you will learn how to transpose the data of the DataTable object and get answers to the following questions.
- Create DataTable programmatically.
- Create Columns with DataType.
- Add rows manually to DataTable.
- Using of StringBuilder.
- Program to convert DataTable data to Transpose.
Create Project
First create Project Asp.Net WebForm.
Right-click on Project and Add a new WebForm page. In our walk-through, we created a WebForm1.aspx page.
First, we work on DataTable.
Note: System.Data namespace required for DataTable.
Create Column
Add Columns: Fullname, City, Phone.
Manually add the rows to the DataTable:
First, we check whether DataTable was created perfectly or not. For that, we bind with GridView Control.
Output
![Transpose-Data-DataTable-DotNet-NET-CSharp-C#]()
Now, we convert DataTable data to Transpose.
We Transpose the DataTable dynamically within two steps, Auto-generate header and fill data as per columns of datatable.
Step 1. Create an HTML Table and populate the table headers with Data Updation.
Step 2. Fill data by replacing [DataCol" + xcolNo.ToString() + "]"
Variable DataCol is replaced with DataVal.
There are the following two methods that complete our transpose functionalities.
- GetHeader: Generate header and write complete table with DataCol
- GetData: This will generate columns within the above table created with GetHeaders.
Output
![Transpose-DataTable-DotNet-NET-CSharp-c#]()
WebForm1.aspx file Code
WebForm1.aspx.cs file code
Happy Coding!