转自: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
分享到:
相关推荐
GridView合并表头单元格 GridView合并表头单元格 GridView合并表头单元格
GridView动态表头合并,GridView动态表头合并,GridView动态表头合并
dev GridView实现多表头合并
GridView合并表头多重表头无错完美版
GridView实现多表头合并
asp.net gridview 合并表头。合并列
ASP.NET 2.0中合并 GridView 的表头单元格
wpf GridView 合并单元格 Demo 简单 适合新手 使用工具vs2017
根据自己需求合并对应的单元格,自定义固定表头。
朋友们使用过程中发现问题告诉我,写了半天了,我赚点辛苦分,嘿嘿,支持一下。
gridview固定表头 横向滚动 纵向固定,即拖动横向滚动条时,表头随着表体移动,纵向拖动滚动条时,表头不动,实践得出来的结果 我用的是VS2010,C#
这个与asp.net中GridView相关的一个实例,实现GridView冻结表头和列,挺好用的。
大家都知道Gridview的合并是比较难的,此实例中把Gridview的单元格轻松合并,多行合并,特别是对于那些要合计的表单有灰常大的作用,代码都在后台控制,没有用前台代码控制表头合并。
GrivView中的单元格合并, 类似于HTML中的跨行或跨列合并单元格,使GridView更方便的展示数据
1.主要利用table style的 layout=fixed属性按照当前表头再copy一个table,显示在当前datagrid或gridview的前面. 2.注意:调用的时候,datagrid 或gridview 要有style="table-layout:fixed" 和 width=?? 属性.
gridview冻结表头和列 gridview冻结表头和列 gridview冻结表头和列 gridview冻结表头和列 gridview冻结表头和列 gridview冻结表头和列
C#语言 GridView创建2行表头,合并表头列代码。
gridview合并单元格,将具有相同内容的单元格合并。
GridView多重表头设计,GridView多重表头设计!