일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 말라키
- build
- SCADA
- Eclipse
- error
- STS
- 파이썬
- Linux
- geckodriver
- Python
- 명령어
- checkbox
- 가상환경
- ORA-28002
- 리눅스
- HMI
- pythoncom37.dll
- 맥코트
- 분노
- 원한
- Anaconda
- DataTables
- LOG
- Custom
- JQuery
- Today
- Total
2010년 5월 1일, 2막
[Tip] Transpose a DataTable using C# 본문
출처 : http://www.codeproject.com/Articles/44274/Transpose-a-DataTable-using-C
데이터 테이블의 행을 열로 바꾸고 싶을 때...
Introduction
This article helps to transpose (convert rows into columns and columns into rows) a DataTable
using C# code in an ASP.NET Web application (can be used in Windows Form as well).
Background
This articles uses .NET Framework 3.5, can be used in any version as DataTable
is applicable to all. Readers should be familiar with basic ASP.NET, C# & DataTable
concepts to understand this article.
Using the Code
This code uses a DataTable
with four columns and three rows as shown below. After calling the method GenerateTransposedTable
, it will give you the output which is shown in the second table.
This can be used in the DataGrid
control (both in Windows and Web applications). Using a single click, we can transpose a table. This may help us in most of the reporting functionalities.
Here, I have used it in an ASP.NET web page.
Actual Table
Status | Phase I | Phase II | Phase III |
Not Started | 100 | 200 | 300 |
Partially Completed | 101 | 201 | 301 |
Successfully Completed | 102 | 202 | 302 |
Blocked | 103 | 203 | 303 |
Completed with Conditions | 104 | 204 | 304 |
Cannot proceed | 105 | 205 | 305 |
Transposed Table
Status | Not Started | Partially Completed | Successfully Completed | Blocked | Completed with Conditions | Cannot proceed |
Phase I | 100 | 101 | 102 | 103 | 104 | 105 |
Phase II | 200 | 201 | 202 | 203 | 204 | 205 |
Phase III | 300 | 301 | 302 | 303 | 304 | 305 |
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridReport.DataSource = ;
// This is the table I shown in Figure 1.1
GridReport.DataBind();
// Your other codes here (if any)
}
}
protected void btnTransposeReport_Click(object sender, EventArgs e)
{
DataTable inputTable = ;
// Table shown in Figure 1.1
DataTable transposedTable = GenerateTransposedTable(inputTable);
GridReport.DataSource = transposedTable;
// Table shown in Figure 1.2
GridReport.DataBind();
}
private DataTable GenerateTransposedTable(DataTable inputTable)
{
DataTable outputTable = new DataTable();
// Add columns by looping rows
// Header row's first column is same as in inputTable
outputTable.Columns.Add(inputTable.Columns[0].ColumnName.ToString());
// Header row's second column onwards, 'inputTable's first column taken
foreach (DataRow inRow in inputTable.Rows)
{
string newColName = inRow[0].ToString();
outputTable.Columns.Add(newColName);
}
// Add rows by looping columns
for (int rCount = 1; rCount <= inputTable.Columns.Count - 1; rCount++)
{
DataRow newRow = outputTable.NewRow();
// First column is inputTable's Header row's second column
newRow[0] = inputTable.Columns[rCount].ColumnName.ToString();
for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
{
string colValue = inputTable.Rows[cCount][rCount].ToString();
newRow[cCount + 1] = colValue;
}
outputTable.Rows.Add(newRow);
}
return outputTable;
}
Conclusion
You can notice that the actual report is transposed (rows into columns and columns into rows) as shown in the figure.
'Computer' 카테고리의 다른 글
[C#] Excel 파일을 DataTable에 담기 (0) | 2014.01.28 |
---|---|
[Tip] 프린터 PCL? PS? (0) | 2014.01.17 |
[Tip] '이 Windows는 정품이 아닙니다.' in Lenovo (0) | 2013.12.23 |
[C#] 첨자 표현법 (0) | 2013.12.11 |
[Java] POI 사용시 formula cell의 결과값이 제대로 나오지 않을 때 (0) | 2013.08.13 |