GridView合并表头多重表头无错完美版(以合并3列3行举例)
后台代码:
前台:
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 using System.Data.SqlClient;
11 using System.Drawing;
12 public partial class _Default : System.Web.UI.Page
13 {
14 SqlConnection sqlcon;
15 SqlCommand sqlcom;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22
23 }
24 }
25 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
26 {
27 GridView1.EditIndex = e.NewEditIndex;
28 bind();
29 }
30 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
31 {
32 sqlcon = new SqlConnection(strCon);
33 string sqlstr = "update 飞狐工作室 set 姓名='"
34 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
35 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
36 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
37 sqlcom=new SqlCommand(sqlstr,sqlcon);
38 sqlcon.Open();
39 sqlcom.ExecuteNonQuery();
40 sqlcon.Close();
41 GridView1.EditIndex = -1;
42 bind();
43 }
44 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
45 {
46 GridView1.EditIndex = -1;
47 bind();
48 }
49 public void bind()
50 {
51 string sqlstr = "select top 10 * from 飞狐工作室";
52 sqlcon = new SqlConnection(strCon);
53 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
54 DataSet myds = new DataSet();
55 sqlcon.Open();
56 myda.Fill(myds, "飞狐工作室");
57 GridView1.DataSource = myds;
58 GridView1.DataKeyNames = new string[] { "身份证号码" };
59 GridView1.DataBind();
60 sqlcon.Close();
61 }
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 using System.Data.SqlClient;
11 using System.Drawing;
12 public partial class _Default : System.Web.UI.Page
13 {
14 SqlConnection sqlcon;
15 SqlCommand sqlcom;
16 string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 if (!IsPostBack)
20 {
21 bind();
22
23 }
24 }
25 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
26 {
27 GridView1.EditIndex = e.NewEditIndex;
28 bind();
29 }
30 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
31 {
32 sqlcon = new SqlConnection(strCon);
33 string sqlstr = "update 飞狐工作室 set 姓名='"
34 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
35 + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
36 + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
37 sqlcom=new SqlCommand(sqlstr,sqlcon);
38 sqlcon.Open();
39 sqlcom.ExecuteNonQuery();
40 sqlcon.Close();
41 GridView1.EditIndex = -1;
42 bind();
43 }
44 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
45 {
46 GridView1.EditIndex = -1;
47 bind();
48 }
49 public void bind()
50 {
51 string sqlstr = "select top 10 * from 飞狐工作室";
52 sqlcon = new SqlConnection(strCon);
53 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
54 DataSet myds = new DataSet();
55 sqlcon.Open();
56 myda.Fill(myds, "飞狐工作室");
57 GridView1.DataSource = myds;
58 GridView1.DataKeyNames = new string[] { "身份证号码" };
59 GridView1.DataBind();
60 sqlcon.Close();
61 }
1 //这里就是解决方案
2 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
3 {
4 switch (e.Row.RowType)
5 {
6 case DataControlRowType.Header:
7 //第一行表头
8 TableCellCollection tcHeader = e.Row.Cells;
9 tcHeader.Clear();
10 tcHeader.Add(new TableHeaderCell());
11 tcHeader[0].Attributes.Add("rowspan", "3"); //跨Row
12 tcHeader[0].Attributes.Add("bgcolor", "white");
13 tcHeader[0].Text = "";
14 tcHeader.Add(new TableHeaderCell());
15 //tcHeader[1].Attributes.Add("bgcolor", "Red");
16 tcHeader[1].Attributes.Add("colspan", "6"); //跨Column
17 tcHeader[1].Text = "全部信息</th></tr><tr>";
18
19 //第二行表头
20 tcHeader.Add(new TableHeaderCell());
21 tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen");
22 tcHeader[2].Text = "身份证号码";
23 tcHeader.Add(new TableHeaderCell());
24 tcHeader[3].Attributes.Add("bgcolor", "LightSteelBlue");
25 tcHeader[3].Attributes.Add("colspan", "2");
26 tcHeader[3].Text = "基本信息";
27 tcHeader.Add(new TableHeaderCell());
28 tcHeader[4].Attributes.Add("bgcolor", "DarkSeaGreen");
29 tcHeader[4].Text = "福利";
30 tcHeader.Add(new TableHeaderCell());
31 tcHeader[5].Attributes.Add("bgcolor", "LightSteelBlue");
32 tcHeader[5].Attributes.Add("colspan", "2");
33 tcHeader[5].Text = "联系方式</th></tr><tr>";
34
35 //第三行表头
36 tcHeader.Add(new TableHeaderCell());
37 tcHeader[6].Attributes.Add("bgcolor", "Khaki");
38 tcHeader[6].Text = "身份证号码";
39 tcHeader.Add(new TableHeaderCell());
40 tcHeader[7].Attributes.Add("bgcolor", "Khaki");
41 tcHeader[7].Text = "姓名";
42 tcHeader.Add(new TableHeaderCell());
43 tcHeader[8].Attributes.Add("bgcolor", "Khaki");
44 tcHeader[8].Text = "出生日期";
45 tcHeader.Add(new TableHeaderCell());
46 tcHeader[9].Attributes.Add("bgcolor", "Khaki");
47 tcHeader[9].Text = "薪水";
48 tcHeader.Add(new TableHeaderCell());
49 tcHeader[10].Attributes.Add("bgcolor", "Khaki");
50 tcHeader[10].Text = "家庭住址";
51 tcHeader.Add(new TableHeaderCell());
52 tcHeader[11].Attributes.Add("bgcolor", "Khaki");
53 tcHeader[11].Text = "邮政编码";
54 break;
55 }
56 }
57 }
2 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
3 {
4 switch (e.Row.RowType)
5 {
6 case DataControlRowType.Header:
7 //第一行表头
8 TableCellCollection tcHeader = e.Row.Cells;
9 tcHeader.Clear();
10 tcHeader.Add(new TableHeaderCell());
11 tcHeader[0].Attributes.Add("rowspan", "3"); //跨Row
12 tcHeader[0].Attributes.Add("bgcolor", "white");
13 tcHeader[0].Text = "";
14 tcHeader.Add(new TableHeaderCell());
15 //tcHeader[1].Attributes.Add("bgcolor", "Red");
16 tcHeader[1].Attributes.Add("colspan", "6"); //跨Column
17 tcHeader[1].Text = "全部信息</th></tr><tr>";
18
19 //第二行表头
20 tcHeader.Add(new TableHeaderCell());
21 tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen");
22 tcHeader[2].Text = "身份证号码";
23 tcHeader.Add(new TableHeaderCell());
24 tcHeader[3].Attributes.Add("bgcolor", "LightSteelBlue");
25 tcHeader[3].Attributes.Add("colspan", "2");
26 tcHeader[3].Text = "基本信息";
27 tcHeader.Add(new TableHeaderCell());
28 tcHeader[4].Attributes.Add("bgcolor", "DarkSeaGreen");
29 tcHeader[4].Text = "福利";
30 tcHeader.Add(new TableHeaderCell());
31 tcHeader[5].Attributes.Add("bgcolor", "LightSteelBlue");
32 tcHeader[5].Attributes.Add("colspan", "2");
33 tcHeader[5].Text = "联系方式</th></tr><tr>";
34
35 //第三行表头
36 tcHeader.Add(new TableHeaderCell());
37 tcHeader[6].Attributes.Add("bgcolor", "Khaki");
38 tcHeader[6].Text = "身份证号码";
39 tcHeader.Add(new TableHeaderCell());
40 tcHeader[7].Attributes.Add("bgcolor", "Khaki");
41 tcHeader[7].Text = "姓名";
42 tcHeader.Add(new TableHeaderCell());
43 tcHeader[8].Attributes.Add("bgcolor", "Khaki");
44 tcHeader[8].Text = "出生日期";
45 tcHeader.Add(new TableHeaderCell());
46 tcHeader[9].Attributes.Add("bgcolor", "Khaki");
47 tcHeader[9].Text = "薪水";
48 tcHeader.Add(new TableHeaderCell());
49 tcHeader[10].Attributes.Add("bgcolor", "Khaki");
50 tcHeader[10].Text = "家庭住址";
51 tcHeader.Add(new TableHeaderCell());
52 tcHeader[11].Attributes.Add("bgcolor", "Khaki");
53 tcHeader[11].Text = "邮政编码";
54 break;
55 }
56 }
57 }
前台:
1
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <html xmlns="http://www.w3.org/1999/xhtml" >
5 <head runat="server">
6 <title>GridView合并多重表头表头 清清月儿http://blog.csdn.net/21aspnet </title>
7 </head>
8 <body >
9 <form id="form1" runat="server">
10 <div >
11 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"
12 OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowCreated="GridView1_RowCreated" >
13 <FooterStyle BackColor="White" ForeColor="#000066" />
14 <Columns>
15 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
16 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
17 <asp:BoundField DataField="姓名" HeaderText="姓名" />
18 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
19 <asp:BoundField DataField="起薪" HeaderText="起薪" />
20 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
21 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
22
23 </Columns>
24 <RowStyle ForeColor="#000066" />
25 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
26 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
27 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
28 </asp:GridView>
29 </div>
30
31 </form>
32 </body>
33 </html>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <html xmlns="http://www.w3.org/1999/xhtml" >
5 <head runat="server">
6 <title>GridView合并多重表头表头 清清月儿http://blog.csdn.net/21aspnet </title>
7 </head>
8 <body >
9 <form id="form1" runat="server">
10 <div >
11 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"
12 OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowCreated="GridView1_RowCreated" >
13 <FooterStyle BackColor="White" ForeColor="#000066" />
14 <Columns>
15 <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
16 <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
17 <asp:BoundField DataField="姓名" HeaderText="姓名" />
18 <asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
19 <asp:BoundField DataField="起薪" HeaderText="起薪" />
20 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
21 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
22
23 </Columns>
24 <RowStyle ForeColor="#000066" />
25 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
26 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
27 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
28 </asp:GridView>
29 </div>
30
31 </form>
32 </body>
33 </html>