1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 |
后台代码: using
System; using
System.Data; using
System.Configuration; using
System.Web; using
System.Web.Security; using
System.Web.UI; using
System.Web.UI.WebControls; using
System.Web.UI.WebControls.WebParts; using
System.Web.UI.HtmlControls; using
System.Data.SqlClient; using
System.Drawing; public
partial class _Default : System.Web.UI.Page { SqlConnection sqlcon; SqlCommand sqlcom; string
strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa" ; protected
void Page_Load( object
sender, EventArgs e) { if
(!IsPostBack) { bind(); } } protected
void GridView1_RowEditing( object
sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected
void GridView1_RowUpdating( object
sender, GridViewUpdateEventArgs e) { sqlcon = new
SqlConnection(strCon); string
sqlstr = "update 飞狐工作室 set 姓名=‘" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "‘,家庭住址=‘" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "‘ where 身份证号码=‘" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "‘" ; sqlcom= new
SqlCommand(sqlstr,sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.EditIndex = -1; bind(); } protected
void GridView1_RowCancelingEdit( object
sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } public
void bind() { string
sqlstr = "select top 10 * from 飞狐工作室" ; sqlcon = new
SqlConnection(strCon); SqlDataAdapter myda = new
SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new
DataSet(); sqlcon.Open(); myda.Fill(myds, "飞狐工作室" ); GridView1.DataSource = myds; GridView1.DataKeyNames = new
string [] { "身份证号码"
}; GridView1.DataBind(); sqlcon.Close(); } //这里就是解决方案 protected
void GridView1_RowCreated( object
sender, GridViewRowEventArgs e) { switch
(e.Row.RowType) { case
DataControlRowType.Header: //第一行表头 TableCellCollection tcHeader = e.Row.Cells; tcHeader.Clear(); tcHeader.Add( new
TableHeaderCell()); tcHeader[0].Attributes.Add( "rowspan" , "3" ); //跨Row tcHeader[0].Attributes.Add( "bgcolor" , "white" ); tcHeader[0].Text = "" ; tcHeader.Add( new
TableHeaderCell()); //tcHeader[1].Attributes.Add("bgcolor", "Red"); tcHeader[1].Attributes.Add( "colspan" , "6" ); //跨Column tcHeader[1].Text = "全部信息</th></tr><tr>" ; //第二行表头 tcHeader.Add( new
TableHeaderCell()); tcHeader[2].Attributes.Add( "bgcolor" , "DarkSeaGreen" ); tcHeader[2].Text = "身份证号码" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[3].Attributes.Add( "bgcolor" , "LightSteelBlue" ); tcHeader[3].Attributes.Add( "colspan" , "2" ); tcHeader[3].Text = "基本信息" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[4].Attributes.Add( "bgcolor" , "DarkSeaGreen" ); tcHeader[4].Text = "福利" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[5].Attributes.Add( "bgcolor" , "LightSteelBlue" ); tcHeader[5].Attributes.Add( "colspan" , "2" ); tcHeader[5].Text = "联系方式</th></tr><tr>" ; //第三行表头 tcHeader.Add( new
TableHeaderCell()); tcHeader[6].Attributes.Add( "bgcolor" , "Khaki" ); tcHeader[6].Text = "身份证号码" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[7].Attributes.Add( "bgcolor" , "Khaki" ); tcHeader[7].Text = "姓名" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[8].Attributes.Add( "bgcolor" , "Khaki" ); tcHeader[8].Text = "出生日期" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[9].Attributes.Add( "bgcolor" , "Khaki" ); tcHeader[9].Text = "薪水" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[10].Attributes.Add( "bgcolor" , "Khaki" ); tcHeader[10].Text = "家庭住址" ; tcHeader.Add( new
TableHeaderCell()); tcHeader[11].Attributes.Add( "bgcolor" , "Khaki" ); tcHeader[11].Text = "邮政编码" ; break ; } } } 前台: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <head runat= "server" > <title>GridView合并多重表头表头</title> </head> <body > <form id= "form1"
runat= "server" > <div > <asp:GridView ID= "GridView1"
runat= "server"
AutoGenerateColumns= "False"
CellPadding= "3"
OnRowEditing= "GridView1_RowEditing" OnRowUpdating= "GridView1_RowUpdating"
OnRowCancelingEdit= "GridView1_RowCancelingEdit"
BackColor= "White"
BorderColor= "#CCCCCC"
BorderStyle= "None"
BorderWidth= "1px"
Font-Size= "12px"
OnRowCreated= "GridView1_RowCreated"
> <FooterStyle BackColor= "White"
ForeColor= "#000066"
/> <Columns> <asp:CommandField HeaderText= "编辑"
ShowEditButton= "True"
/> <asp:BoundField DataField= "身份证号码"
HeaderText= "编号"
ReadOnly= "True"
/> <asp:BoundField DataField= "姓名"
HeaderText= "姓名"
/> <asp:BoundField DataField= "出生日期"
HeaderText= "邮政编码"
/> <asp:BoundField DataField= "起薪"
HeaderText= "起薪"
/> <asp:BoundField DataField= "家庭住址"
HeaderText= "家庭住址"
/> <asp:BoundField DataField= "邮政编码"
HeaderText= "邮政编码"
/> </Columns> <RowStyle ForeColor= "#000066"
/> <SelectedRowStyle BackColor= "#669999"
Font-Bold= "True"
ForeColor= "White"
/> <PagerStyle BackColor= "White"
ForeColor= "#000066"
HorizontalAlign= "Left"
CssClass= "ms-formlabel DataGridFixedHeader" /> <HeaderStyle BackColor= "#006699"
Font-Bold= "True"
ForeColor= "White"
/> </asp:GridView> </div> </form> </body> </html> |
GridView合并表头多重表头,布布扣,bubuko.com
原文:http://www.cnblogs.com/shangshen/p/3592201.html