其实就是行转列,你看看这个吧,即可解决:
C# Pivot Table:http://www.codeproject.com/KB/recipes/CsharpPivotTable.aspx
动态生成GridView,不难吧。
Code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#DE222DF223DE" BorderStyle="None"
BorderWidth="1px" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1"
ForeColor="Black" GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<FooterStyle BackColor="#CC204CC99153" />
<PagerStyle BackColor="#F7 247 F7DE222" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE206 5D93 5A90" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B107 69105 6B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
asp:GridView>
相对应的代码:
Code
GridView gridviewcourse = new GridView();
gridviewcourse.AllowPaging = true;
gridviewcourse.AllowSorting = true;
gridviewcourse.AutoGenerateColumns = false;
gridviewcourse.BackColor = Color.White;
gridviewcourse.BorderColor = Color.FromArgb( 222 , 223 , 222 );
gridviewcourse.BorderStyle = BorderStyle.None;
gridviewcourse.BorderWidth = Unit.Pixel( 1 );
gridviewcourse.CellPadding = 4;
gridviewcourse.DataKeyNames = new string[] { "ID" };
gridviewcourse.DataSource = sds;
gridviewcourse.ForeColor = Color.Black;
gridviewcourse.GridLines = GridLines.Vertical;
gridviewcourse.FooterStyle.BackColor = Color.FromArgb( 204 , 204 , 153 );
gridviewcourse.RowStyle.BackColor = Color.FromArgb( 204 , 204 , 153 );
gridviewcourse.PagerStyle.BackColor = Color.FromArgb( 247 , 247 , 222 );
gridviewcourse.PagerStyle.ForeColor = Color.Black;
gridviewcourse.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
gridviewcourse.SelectedRowStyle.BackColor = Color.FromArgb( 206 , 93 , 90 );
gridviewcourse.SelectedRowStyle.Font.Bold = true;
gridviewcourse.SelectedRowStyle.ForeColor = Color.White;
gridviewcourse.HeaderStyle.BackColor = Color.FromArgb( 107 , 105 , 107 );
gridviewcourse.HeaderStyle.Font.Bold = true;
gridviewcourse.ForeColor = Color.White;
gridviewcourse.AlternatingRowStyle.BackColor = Color.White;
gridviewcourse.SelectedIndexChanged += new EventHandler( GridView1_SelectedIndexChanged );
gridviewcourse.SelectedIndexChanging += new GridViewSelectEventHandler( GridView1_SelectedIndexChanging );
补充:
如何通过程序把一个表的行,作为生成另外一个表的列呢?
先对第一表进行转列,然后把“结果数据”供生成第二表所用。就是这样。
1.table1行转列-->结果装入Entity
2.第二表把Entity中的属性当作字段进行生成。
ex:
public class Entity {
public String 字段A{get;set;}....
}
DataTable dt = new DataTable(); //生成第二表
dt.Columns.Add("字段A", typeof (string));
....
dt.Rows.Add("X", "A", "B", "C", "B");//循环Entity添加数据
...
行转列的答案太多了