`
61party
  • 浏览: 1042506 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

GridView、DataGrid 表头合并、表体合并[合并相邻行相同数据的单元格

阅读更多

转自:http://www.cnblogs.com/ziyan22/archive/2008/03/05/1092681.html


1usingSystem;
2usingSystem.Data;
3usingSystem.Configuration;
4usingSystem.Web;
5usingSystem.Web.Security;
6usingSystem.Web.UI;
7usingSystem.Web.UI.WebControls;
8usingSystem.Web.UI.WebControls.WebParts;
9usingSystem.Web.UI.HtmlControls;
10
11publicpartialclass_Default:System.Web.UI.Page
12{
13protectedvoidPage_Load(objectsender,EventArgse)
14{
15DataTableDT=newDataTable();
16DT.Columns.Add("序号");
17DT.Columns.Add("原油部分");
18DT.Columns.Add("轻烃部分");
19DataRowdr=DT.NewRow();
20dr[0]="1";
21dr[1]="2";
22dr[2]="3";
23DT.Rows.Add(dr);
24
25GridView1.DataSource=DT;
26GridView1.DataBind();
27}

28
29///<summary>
30///合并相邻行相同数据的单元格
31///</summary>
32///<paramname="DataGrid1">DataGrid对象</param>
33///<paramname="ColNum">要合并的列数</param>

34privatevoidSpan(GridViewDataGrid1,intColNum)
35{
36stringtemp="";
37intj=0,intspan;
38intlocal=0;
39for(inti=0;i<DataGrid1.Rows.Count;i++)
40{
41temp=DataGrid1.Rows[i].Cells[ColNum].Text;
42local=i;
43intspan=1;
44for(j=j+1;j<DataGrid1.Rows.Count;j++)
45{
46if(string.Compare(temp,DataGrid1.Rows[j].Cells[ColNum].Text)==0)
47{
48intspan++;
49DataGrid1.Rows[local].Cells[ColNum].RowSpan=intspan;
50DataGrid1.Rows[j].Cells[ColNum].Visible=false;
51}

52else
53{
54temp=DataGrid1.Rows[j].Cells[ColNum].Text;
55local=j;
56intspan=1;
57}

58}

59}

60}

61
62///<summary>
63///合并相邻行相同数据的单元格
64///</summary>
65///<paramname="DataGrid1">DataGrid对象</param>
66///<paramname="ColNum">要合并的列数</param>

67privatevoidSpan(DataGridDataGrid1,intColNum)
68{
69stringtemp="";
70intj=0,intspan;
71intlocal=0;
72for(inti=0;i<DataGrid1.Items.Count;i++)
73{
74temp=DataGrid1.Items[i].Cells[ColNum].Text;
75local=i;
76intspan=1;
77for(j=j+1;j<DataGrid1.Items.Count;j++)
78{
79if(string.Compare(temp,DataGrid1.Items[j].Cells[ColNum].Text)==0)
80{
81intspan++;
82DataGrid1.Items[local].Cells[ColNum].RowSpan=intspan;
83DataGrid1.Items[j].Cells[ColNum].Visible=false;
84}

85else
86{
87temp=DataGrid1.Items[j].Cells[ColNum].Text;
88local=j;
89intspan=1;
90}

91}

92}

93}

94///<summary>
95///合并表头表体
96///</summary>
97///<paramname="sender"></param>
98///<paramname="e"></param>

99protectedvoidGridView1_RowCreated(objectsender,GridViewRowEventArgse)
100{
101if(e.Row.RowType==DataControlRowType.Header)
102{
103for(inti=0;i<e.Row.Cells.Count;i++)
104{
105if(i==0)
106e.Row.Cells[i].RowSpan=3;
107if(i==1)
108e.Row.Cells[i].ColumnSpan=3;
109if(i==2)
110e.Row.Cells[i].ColumnSpan=2;
111}

112TableCellcell=e.Row.Cells[e.Row.Cells.Count-1];
113LiteralControllc=newLiteralControl(cell.Text+"</td></tr>"+GetMegCell());
114cell.Controls.Add(lc);
115}

116elseif(e.Row.RowType==DataControlRowType.DataRow||e.Row.RowType==DataControlRowType.EmptyDataRow)
117{
118e.Row.Cells.AddAt(3,tc("3"));
119e.Row.Cells.AddAt(4,tc("4"));
120e.Row.Cells.AddAt(5,tc("5"));
121}

122}

123
124privateTableCelltc(stringva)
125{
126TableCelltc1=newTableCell();
127tc1.Text=va;
128returntc1;
129}

130///<summary>
131///欲合并后的表头HTML
132///</summary>
133///<returns></returns>

134privatestringGetMegCell()
135{
136return"<tr><tdwidth=189colspan=2align=\"center\"><b>轻烃计算</b></td><tdwidth=95rowspan=2align=center><b>合计</b></td><tdwidth=95align=center><b>采气部分</b></td><tdwidth=95align=center><b>销售部分</b></td></tr><tr><tdwidth=95align=center><b>日计算</b></td><tdwidth=95align=center><b>月计算</b></td><tdwidth=95align=center><b>日数据</b></td><tdwidth=95align=center><b>日数据</b></td>";
137}

138}

139

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics