惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
博客园_首页
J
Java Code Geeks
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
量子位
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
N
Netflix TechBlog - Medium
Hugging Face - Blog
Hugging Face - Blog
B
Blog
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI

博客园 - Kejames

OSQL Command. Another version for the missing method: Enum.TryParse (in C#) Form validation before onClientClick. Defining a Version String RegistryKey取得系統Media path. Enum.GetValues() About HashTable sort. 【轉載】自定義應用程序配置文件(app.config) 【轉載】二位高手所寫的OleDb Help Class [轉載]11 Visual Studio 2005 IDE Tips and Tricks to Make You a More Productive Developer [轉載]Automated Class Builder for Database Tables [轉載]HyperNetDatabase [轉載]Generate SQL INSERT commands Programmatically [轉載]SQLDataAdapter without using SQLCommandBuilder [轉載]去除代码行号的一个小程序 【動腦時間】要如何交換兩個變數,而不動用第三個變數? 網站程式安全 - 技術門檻高, 攻擊門檻低! 企業棄守!! Fiddler2 - JavaScript Beautifier Plugin Microsoft SQL Server Enterprise Manager 無法開啟的解決方式
[轉載]Displaying vertical rows in a DataGrid
Kejames · 2007-09-27 · via 博客园 - Kejames

 以前遇到要資料表要轉置時,總是要很麻煩的下SQL來轉換,現在ASP.Net提供了強大的DataTable,所以遇到要轉置的時候,用DataTable來轉置,方便又簡單喔。
---

Introduction

Last week, when I wanted to display a DataGrid in a horizontal manner (vertical rows), I thought there may be some property in DataGrid to flip it, but it wasn’t the case or may be I couldn’t find it. I read online articles and forums but didn’t get very useful or easy solutions. One of the solutions I found was to nest a DataList into a DataSet but I didn’t find this easy; and the other was all together a new control called xrepeater but I found mine easier than the two of them.

Let’s say you have a dataset which you want to render through a datagrid but you don’t want to show horizontal rows, instead you want to show the rows vertically. There may be lot of cases when you want to do that, for example comparison between two things on several criteria (Column headings) or when you have just one or two rows to show then you would prefer the vertical rows for more clarity. Here is how the idea of flipping the table comes in.

The idea is to flip all the entries inside each table of the dataset. This is not a very good solution but it works fine. I wrote a method which takes a DataSet as a parameter and returns a new DataSet with all tables flipped and now you just have to bind this DataSet’s tables to the DataGrid as you do normally and DataGrid renders vertical rows.

Old DataSet

Old Table looks like below.

Column1 Column2 Column3
Neeraj Jain Carios
Tashan Yen Agknow
Andrew Ferriere Feedback

Flipped DataSet

New DataTable looks like this

0 1 2 3
Column1 Neeraj Tashan Andrew
Column2 Jain Yen Agknow
Column3 Carios Agknow Feedback

Code

Here is the code for FlipDataSet method, is in the code behind file for me. In method FlipDataSet, I am doing flipping of dataset and in BindData, I am binding the first table of it to the DataGrid. This is all:

private void BindData()
{
    DataSet ds 
= this.GetDetail(); // Some DataSet
    DataSet new_ds = FlipDataSet(ds); // Flip the DataSet
    DataView my_DataView = new_ds.Tables[0].DefaultView;
    
this.my_DataGrid.DataSource = my_DataView;
    
this.my_DataGrid.DataBind();
}



public DataSet FlipDataSet(DataSet my_DataSet)
{
    DataSet ds 
= new DataSet();
    
foreach(DataTable dt in my_DataSet.Tables)
    
{
        DataTable table 
= new DataTable();
        
for(int i=0; i<=dt.Rows.Count; i++)
        
{
            table.Columns.Add(Convert.ToString(i));
        }

        DataRow r;
        
for(int k=0; k<dt.Columns.Count; k++)
        
{
            r 
= table.NewRow();
            r[
0= dt.Columns[k].ToString();
            
for(int j=1; j<=dt.Rows.Count; j++)
                r[j] 
= dt.Rows[j-1][k];
        }

        table.Rows.Add(r);
    }

    ds.Tables.Add(table);
}

return ds;
}


---
From : http://www.codeproject.com/cs/database/Vertical_rows_in_Datagrid.asp