Gridview, DataGrid header merge, table body merge [merge cells with the same data of adjacent rows

Source: Internet
Author: User

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


1 using system;
2 using system. Data;
3 using system. configuration;
4 using system. Web;
5 using system. Web. Security;
6 using system. Web. UI;
7 using system. Web. UI. webcontrols;
8 using system. Web. UI. webcontrols. webparts;
9 using system. Web. UI. htmlcontrols;
10
11 public partial class _ default: system. Web. UI. Page
12 {
13 protected void page_load (Object sender, eventargs E)
14 {
15 datatable dt = new datatable ();
16 DT. Columns. Add ("no ");
17 DT. Columns. Add ("crude oil ");
18 DT. Columns. Add ("Light Hydrocarbon part ");
19 datarow DR = 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 /// merge cells with the same data of adjacent rows
31 /// </Summary>
32 // <Param name = "datagrid1"> DataGrid object </param>
33 // <Param name = "colnum"> Number of columns to be merged </param>
34 Private void span (gridview datagrid1, int colnum)
35 {
36 string temp = "";
37 Int J = 0, intspan;
38 int local = 0;
39 For (INT I = 0; I <datagrid1.rows. Count; I ++)
40 {
41 temp = maid [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 = maid [J]. cells [colnum]. text;
55 local = J;
56 intspan = 1;
57}
58}
59}
60}
61
62 // <summary>
63 // merge cells with the same data of adjacent rows
64 /// </Summary>
65 // <Param name = "datagrid1"> DataGrid object </param>
66 // <Param name = "colnum"> Number of columns to be merged </param>
67 private void span (DataGrid datagrid1, int colnum)
68 {
69 string temp = "";
70 Int J = 0, intspan;
71 int local = 0;
72 for (INT I = 0; I <datagrid1.items. Count; I ++)
73 {
74 temp = maid [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 maid [local]. cells [colnum]. rowspan = intspan;
83 datagrid1.items [J]. cells [colnum]. Visible = false;
84}
85 else
86 {
87 temp = maid [J]. cells [colnum]. text;
88 local = J;
89 intspan = 1;
90}
91}
92}
93}
94 /// <summary>
95 // merge the table Header
96 /// </Summary>
97 // <Param name = "sender"> </param>
98 // <Param name = "E"> </param>
99 protected void gridview1_rowcreated (Object sender, gridviewroweventargs E)
100 {
101 if (E. Row. rowtype = datacontrolrowtype. header)
102 {
103 for (INT I = 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 tablecell cell = E. Row. cells [E. Row. cells. Count-1];
113 literalcontrol lc = new literalcontrol (cell. Text + "</TD> </tr>" + getmegcell ());
114 cell. Controls. Add (LC );
115}
116 else if (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 private tablecell Tc (string VA)
125 {
126 tablecell TC1 = new tablecell ();
127 tc1.text = VAS;
128 return TC1;
129}
130 /// <summary>
131 // the header HTML to be merged
132 /// </Summary>
133 /// <returns> </returns>
134 private string getmegcell ()
135 {
136 return "<tr> <TD width = 189 colspan = 2 align =/" center/"> <B> light hydrocarbon calculation </B> </TD> <TD width = 95 rowspan = 2 align = center> <B> total </B> </TD> <TD width = 95 align = center> <B> gas collection </B> </TD> <TD width = 95 align = center> <B> sales </B> </TD> </tr> <TD width = 95 align = center> <b> daily calculation </B> </TD> <TD width = 95 align = center> <B> monthly calculation </B> </TD> <TD width = 95 align = center> <B> daily data </B> </TD> <TD width = 95 align = center> <B> daily data </B> </TD> ";
137}
138}
139

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.