Multiple row headers are merged and there are many instances on the Internet. The details are detailed here, so that everyone can understand them.
Implementation principle: display the table header of the HMTL table in ASP. NET.
When creating a Row Header Row in the GridView: e. Row. RowType = DatacontrolRowType. Header
Clear the old header, and then splice the new header.
TableHeaderCell thc = new TableHeaderCell ();
Thc. Text = "Header ";
The generated HTML is: <th> header </th>
Merge multiple row headers
Test multi-row merge Header |
Header |
Header 1 |
Header 2 |
Header 3 |
Header 1-1 |
Header 2-1 |
Header 2-2 |
Header 3-1 |
Header 3-2 |
Header 3-3 |
1 protected void gridview1_rowcreated (Object sender, gridviewroweventargs E)
2 {
3 // determine whether the created row is a header row
4 If (E. Row. rowtype = datacontrolrowtype. header)
5 {
6 // obtain all cells in the row where the header is located
7 tablecellcollection tcheader = E. Row. cells;
8 // clear automatically generated Headers
9 tcheader. Clear ();
10
11 // the newly added first header cell is set to merge seven columns to form a row.
12 tcheader. Add (New tableheadercell ());
13 tcheader [0]. columnspan = 7;
14 tcheader [0]. Text = "test multi-row merge header </Th> </tr> <tr> ";
15 // </Th> indicates that the current cell ends, </tr> indicates that the row ends, and <tr> starts a new key line.
16
17 // Add the second header cell and set it to merge the two rows.
18 tcheader. Add (New tableheadercell ());
19 tcheader [1]. rowspan = 2;
20 tcheader [1]. Text = "Header ";
21
22 tcheader. Add (New tableheadercell ());
23 tcheader [2]. Text = "header 1 ";
24
25 tcheader. Add (New tableheadercell ());
26 tcheader [3]. columnspan = 2;
27 tcHeader [3]. Text = "header 2 ";
28
29 tcHeader. Add (new TableHeaderCell ());
30 tcHeader [4]. ColumnSpan = 3;
31 tcHeader [4]. Text = "header 3 </th> </tr> <tr> ";
32
33 // all cells in the second row are added, with a line break </th> </tr> <tr>
34
35 // Add all cells in the third row
36 tcHeader. Add (new TableHeaderCell ());
37 tcHeader [5]. Text = "header 1-1 ";
38
39 tcHeader. Add (new TableHeaderCell ());
40 tcHeader [6]. Text = "header 2-1 ";
41
42 tcHeader. Add (new TableHeaderCell ());
43 tcHeader [7]. Text = "header 2-2 ";
44
45 tcHeader. Add (new TableHeaderCell ());
46 tcHeader [8]. Text = "header 3-1 ";
47
48 tcHeader. Add (new TableHeaderCell ());
49 tcHeader [9]. Text = "header 3-2 ";
50
51 tcHeader. Add (new TableHeaderCell ());
52 tcHeader [10]. Text = "header 3-3 </th> </tr> <tr> ";
53}
54
55}