转自:http://www.cnblogs.com/ziyan22/archive/2008/03/05/1092681.html
1
usingSystem;
2
usingSystem.Data;
3
usingSystem.Configuration;
4
usingSystem.Web;
5
usingSystem.Web.Security;
6
usingSystem.Web.UI;
7
usingSystem.Web.UI.WebControls;
8
usingSystem.Web.UI.WebControls.WebParts;
9
usingSystem.Web.UI.HtmlControls;
10
11
publicpartialclass_Default:System.Web.UI.Page
12
{
13
protectedvoidPage_Load(objectsender,EventArgse)
14
{
15
DataTableDT=newDataTable();
16
DT.Columns.Add("序号");
17
DT.Columns.Add("原油部分");
18
DT.Columns.Add("轻烃部分");
19
DataRowdr=DT.NewRow();
20
dr[0]="1";
21
dr[1]="2";
22
dr[2]="3";
23
DT.Rows.Add(dr);
24
25
GridView1.DataSource=DT;
26
GridView1.DataBind();
27
}
28
29
///<summary>
30
///合并相邻行相同数据的单元格
31
///</summary>
32
///<paramname="DataGrid1">DataGrid对象</param>
33
///<paramname="ColNum">要合并的列数</param>
34
privatevoidSpan(GridViewDataGrid1,intColNum)
35
{
36
stringtemp="";
37
intj=0,intspan;
38
intlocal=0;
39
for(inti=0;i<DataGrid1.Rows.Count;i++)
40
{
41
temp=DataGrid1.Rows[i].Cells[ColNum].Text;
42
local=i;
43
intspan=1;
44
for(j=j+1;j<DataGrid1.Rows.Count;j++)
45
{
46
if(string.Compare(temp,DataGrid1.Rows[j].Cells[ColNum].Text)==0)
47
{
48
intspan++;
49
DataGrid1.Rows[local].Cells[ColNum].RowSpan=intspan;
50
DataGrid1.Rows[j].Cells[ColNum].Visible=false;
51
}
52
else
53
{
54
temp=DataGrid1.Rows[j].Cells[ColNum].Text;
55
local=j;
56
intspan=1;
57
}
58
}
59
}
60
}
61
62
///<summary>
63
///合并相邻行相同数据的单元格
64
///</summary>
65
///<paramname="DataGrid1">DataGrid对象</param>
66
///<paramname="ColNum">要合并的列数</param>
67
privatevoidSpan(DataGridDataGrid1,intColNum)
68
{
69
stringtemp="";
70
intj=0,intspan;
71
intlocal=0;
72
for(inti=0;i<DataGrid1.Items.Count;i++)
73
{
74
temp=DataGrid1.Items[i].Cells[ColNum].Text;
75
local=i;
76
intspan=1;
77
for(j=j+1;j<DataGrid1.Items.Count;j++)
78
{
79
if(string.Compare(temp,DataGrid1.Items[j].Cells[ColNum].Text)==0)
80
{
81
intspan++;
82
DataGrid1.Items[local].Cells[ColNum].RowSpan=intspan;
83
DataGrid1.Items[j].Cells[ColNum].Visible=false;
84
}
85
else
86
{
87
temp=DataGrid1.Items[j].Cells[ColNum].Text;
88
local=j;
89
intspan=1;
90
}
91
}
92
}
93
}
94
///<summary>
95
///合并表头表体
96
///</summary>
97
///<paramname="sender"></param>
98
///<paramname="e"></param>
99
protectedvoidGridView1_RowCreated(objectsender,GridViewRowEventArgse)
100
{
101
if(e.Row.RowType==DataControlRowType.Header)
102
{
103
for(inti=0;i<e.Row.Cells.Count;i++)
104
{
105
if(i==0)
106
e.Row.Cells[i].RowSpan=3;
107
if(i==1)
108
e.Row.Cells[i].ColumnSpan=3;
109
if(i==2)
110
e.Row.Cells[i].ColumnSpan=2;
111
}
112
TableCellcell=e.Row.Cells[e.Row.Cells.Count-1];
113
LiteralControllc=newLiteralControl(cell.Text+"</td></tr>"+GetMegCell());
114
cell.Controls.Add(lc);
115
}
116
elseif(e.Row.RowType==DataControlRowType.DataRow||e.Row.RowType==DataControlRowType.EmptyDataRow)
117
{
118
e.Row.Cells.AddAt(3,tc("3"));
119
e.Row.Cells.AddAt(4,tc("4"));
120
e.Row.Cells.AddAt(5,tc("5"));
121
}
122
}
123
124
privateTableCelltc(stringva)
125
{
126
TableCelltc1=newTableCell();
127
tc1.Text=va;
128
returntc1;
129
}
130
///<summary>
131
///欲合并后的表头HTML
132
///</summary>
133
///<returns></returns>
134
privatestringGetMegCell()
135
{
136
return"<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
本文介绍了一个 ASP.NET 中使用 GridView 控件进行数据展示的例子,并演示了如何通过编程方式实现对 GridView 中重复数据的单元格进行合并。
686

被折叠的 条评论
为什么被折叠?



