现在的位置: 首页 > 综合 > 正文

GridView多表头固定+分组+总计

2013年10月31日 ⁄ 综合 ⁄ 共 7267字 ⁄ 字号 评论关闭

一直在网上找资料来弄个固定表头的东西 有时也想根据有字段来对数据进行分组。

 完成了一个项目也把一些经常见到的需求提炼出了一个还比较可以的解决方案。

常见需求:

   1、列表显示数据

   2、滚动滚动条时表头固定

   3、按要求将数据分组并计算求和

这些问题在财务系统中经常遇到相比在其他行业也有类似情况,今天下午整理了下资料,来出来和大家分享。

  效果图1:

 

 效果图2 :含总计行

 

 

代码:

  页面类:

代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView固定表头+分组+固定</title>
<style type="text/css">
.staicTitlteDiv{
WIDTH: 1000px;
height:500px ;
OVERFLOW: scroll;
SCROLLBAR
-FACE-COLOR: EAECEC;
SCROLLBAR
-HIGHLIGHT-COLOR: EAECEC;
SCROLLBAR
-SHADOW-COLOR: BLACK;
SCROLLBAR
-3DLIGHT-COLOR: EAECEC;
SCROLLBAR
-ARROW-COLOR: EAECEC;
SCROLLBAR
-TRACK-COLOR: FFFFFF;
SCROLLBAR
-DARKSHADOW-COLOR: EAECEC;

}
.fixedHeaderTr
{
position:relative;
border:
0;
top:expression(
this.offsetParent.scrollTop);

}
th{
position:relative;
background
-color: Beige;
top:expression(
this.offsetParent.scrollTop);
}
</style>

</head>
<body style="text-align:center">
<form id="form1" runat="server" >
<div class="staicTitlteDiv">
<asp:GridView ID="GridView1" runat="server">
<HeaderStyle CssClass="fixedHeaderTr" BorderColor="Beige" />
</asp:GridView>
</div>
</form>
</body>
</html>

 后置代码:

代码

1 using System;
2  using System.Collections;
3  using System.Configuration;
4  using System.Data;
5  using System.Linq;
6  using System.Web;
7  using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Collections.Generic;
14 using System.Linq;
15 public partial class Default2 : System.Web.UI.Page
16 {
17
18 protected void Page_Load(object sender, EventArgs e)
19 {
20 if (!IsPostBack)
21 {
22 GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
23 GridView1.DataBound += new EventHandler(GridView_DataBound);
24 GridView1DataBind();
25
26
27 }
28 }
29 void GridView1DataBind()
30 {
31 DataTable table = new DataTable();
32 table.Columns.Add("class", typeof(string)).SetOrdinal(0);
33 table.Columns.Add("student", typeof(string)).SetOrdinal(1);
34 table.Columns.Add("Languages", typeof(Double)).SetOrdinal(2);
35 table.Columns.Add("Rate1", typeof(Double)).SetOrdinal(3);
36 table.Columns.Add("Mathematics", typeof(Double)).SetOrdinal(4);
37 table.Columns.Add("Rate2", typeof(Double)).SetOrdinal(5);
38 table.Columns.Add("English", typeof(Double)).SetOrdinal(6);
39 table.Columns.Add("Rate3", typeof(Double)).SetOrdinal(7);
40 table.Columns.Add("Other", typeof(Double)).SetOrdinal(8);
41 table.Columns.Add("TotalScore", typeof(Double), "ISNULL(Languages,0)+ISNULL(Mathematics,0)+ISNULL(English,0)+ISNULL(Other,0)").SetOrdinal(9);
42
43 table.Columns[3].Expression = "Languages/TotalScore";
44 table.Columns[5].Expression ="Mathematics/TotalScore";
45 table.Columns[7].Expression = "English/TotalScore";
46 Random random = new Random();
47 for (int i = 0; i < 100; i++)
48 {
49 DataRow row = table.NewRow();
50 row["student"] = "学生" + i;
51 row["class"] = "班级" + random.Next(0, 10);
52 row["Languages"] = random.Next(0, 151);
53 row["Mathematics"] = random.Next(0, 151);
54 row["English"] = random.Next(0, 151);
55 row["Other"] = random.Next(0, 150*3+1);
56 table.Rows.Add(row);
57 }
58
59 //GridView1.DataSource =GridViewGroup.AddGurop(table, "class");
60 GridView1.DataSource = GridViewGroup.AddGurop(table, "class", "Languages#Mathematics#English#Other");
61 GridView1.DataBind();
62
63 }
64 void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
65 {
66 if (e.Row.RowType == DataControlRowType.Header)
67 {
68 string header = "班级#学生#其中 语文 成绩,比例#其中 数学 成绩,比例#其中 英语 成绩,比例#其中 &nbsp; 其他3科#总成绩";
69 AndyGridViewTHeaderHepler help = new AndyGridViewTHeaderHepler();
70 e.Row.CssClass = "fixedHeaderTr";
71
72 e.Row.BackColor = System.Drawing.Color.Beige;
73 help.SplitTableHeader(e.Row, header);
74 }
75 else
76 {
77 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
78 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=/"" + "#E7E7FF" + "/"");
79
80 }
81 }
82
83 protected void GridView_DataBound(object sender, EventArgs e)
84 {
85 int index = -1;
86 int startGroupIndex = 0;
87 foreach (GridViewRow row in GridView1.Rows)
88 {
89 index++;
90
91 if (index > 0 && (row.Cells[0].Text.ToString().Trim() == "" || row.Cells[0].Text.ToString().Trim() == "&nbsp;"))
92 {
93 row.Cells[0].Text = "小计";// GridView1.Rows[startGroupIndex].Cells[0].Text;
94 row.Attributes.Add("style", "background-color: #F2F2F2; font-weight: bold;font-variant: normal; color: #000000;text-align:center;");
95 row.Attributes.Remove("onmouseout");
96 row.Attributes.Remove("onmouseover");
97 GridViewGroup.GroupRow(GridView1, index, 0, 2);
98 GridViewGroup.GropCell(GridView1, 0, startGroupIndex, index - 1);
99 startGroupIndex = index + 1;
100 }
101 }
102 GridViewRow sumRow = GridView1.Rows[index];
103 sumRow.Attributes.Add("style", "background-color: #EAEAEA;font-weight: bold;font-variant: normal;color: #000000;text-align: center;");
104 sumRow.Attributes.Remove("onmouseout");
105 sumRow.Attributes.Remove("onmouseover");
106 GridViewGroup.GroupRow(GridView1, index, 0, 2);
107 }
108 }
109

 3.多表头帮助类(来自网络)

AndyGridViewTHeaderHepler.cs

1
2 using System;
3 using System.Data;
4 using System.Configuration;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Collections.Generic;
12 using System.Collections;
13
14
15 //-----------------------------------------------------------------------------------
16 //
17 // Created: 23:07 2008/9/28 Author: Andy Lu
18 // File: AndyGridViewTHeaderHepler.cs
19 // Description: 动态生成复合表头帮助类
20 // 相邻父列头之间用'#'分隔,父列头与子列头用空格(' ')分隔,相邻子列头用逗号分隔(',').
21 // 两行:序号#分公司#组别#本日成功签约单数 预警,续约,流失,合计#累计成功签约单数 预警,续约,流失,合计#任务数#完成比例#排名
22 // 三行:等级#级别#上期结存 件数, 重量,比例#本期调入 收购调入 件数,重量,比例#本期发出 车间投料 件数,重量,比例#本期发出 产品外销百分比 件数,重量,比例#平均值
23 // 三行时请注意:列头要重复
24 //
25 //-----------------------------------------------------------------------------------
26 public class AndyGridViewTHeaderHepler
27 {
28 public AndyGridViewTHeaderHepler()
29 {
30 //
31 // TODO: Add constructor logic here
32 //
33 }
34 /**/
35 /// <summary>
36 /// 重写表头
37 /// </summary>
38 /// <param name="targetHeader">目标表头</param>
39 /// <param name="newHeaderNames">新表头</param>
40 /// <remarks>
41 /// </remarks>
42 public void SplitTableHeader(GridViewRow targetHeader, string newHeaderNames)
43 {
44 TableCellCollection tcl = targetHeader.Cells; //获得表头元素的实例
45 tcl.Clear(); //清除元素
46 int row = GetRowCount(newHeaderNames);
47 int col = GetColCount(newHeaderNames);
48 string[,] nameList = ConvertList(newHeaderNames, row, col);
49 int RowSpan = 0;
50 int ColSpan = 0;
51 for (int k = 0; k < row; k++)
52 {
53 string LastFName = "";
54 for (int i = 0; i < col; i++)
55 {
56 if (LastFName == nameList[i, k] && k != row - 1)
57 {
58 LastFName = nameList[i, k];
59 continue;
60 }
61 else
62 {
63 LastFName = nameList[i, k];
64 }
65 int bFlag = IsVisible(nameList, k, i, LastFName);
66 switch (bFlag)
67 {
68 case 0:
69 break;
70 case 1:
71 RowSpan = GetSpanRowCount(nameList, row, k, i);
72 ColSpan = GetSpanColCount(nameList, row, col, k, i);
73 tcl.Add(new TableHeaderCell()); //添加表头控件
74 tcl[tcl.Count - 1].RowSpan = RowSpan;
75 tcl[tcl.Count - 1].ColumnSpan = ColSpan;
76 tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
77 tcl[tcl.Count - 1].Text = LastFName;
78 break;
79 case -1:
80 string[] EndColName = LastFName.Split(new char[] { ',' });
81 foreach (string eName in EndColName)
82 {
83 tcl.Add(new TableHeaderCell()); //添加表头控件
84 tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
85 tcl[tcl.Count - 1].Text = eName;
86 }
87 break;
88 }
89 }
90 if (k != row - 1)
91 { //不是起始行,加入新行标签
92 tcl[tcl.Count - 1].Text = tcl[tcl.Count - 1].Text + "</th></tr><tr class=/"" + targetHeader.CssClass + "/">";
93 }
94 }
95 }
96 /**/
97 /// <summary>
98 /// 重写表头
99 /// </summary>
100 ///

抱歉!评论已关闭.