工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

前台代码:

View Code
1 <asp:GridView ID="GridView1" runat="server" BorderColor="Black" AutoGenerateColumns="False" OnRowCreated="GridView1_RowCreated"
2 OnRowDataBound="GridView1_RowDataBound">
3 <Columns>
4 <asp:BoundField DataField="编号" HeaderText="编号" />
5 <asp:BoundField DataField="姓名" HeaderText="姓名" />
6 <asp:BoundField DataField="出生年月" HeaderText="出生年月" />
7 <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
8 <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
9 <asp:BoundField DataField="起薪" HeaderText="起薪" />
10 </Columns>
11 </asp:GridView>

后台代码:

View Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 databind();
6 }
7
8 }
9 public void databind()
10 {
11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
12 SqlCommand cmd = new SqlCommand();
13 cmd.Connection = con;
14 cmd.CommandText = "select * from T_Message";
15 SqlDataAdapter da = new SqlDataAdapter(cmd);
16 DataSet ds = new DataSet();
17 da.Fill(ds);
18 this.GridView1.DataSource = ds.Tables[0];
19 this.GridView1.DataBind();
20 }
21 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
22 {
23 foreach (TableCell item in e.Row.Cells)
24 {
25 item.Attributes.Add("style", "border-color:black");
26
27 }
28 }
29 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
30 {
31 if (e.Row.RowType == DataControlRowType.Header)
32 {
33 TableCellCollection tc = e.Row.Cells;
34 tc.Clear();
35 tc.Add(new TableHeaderCell());
36 tc[0].Attributes.Add("colspan", "6");
37 tc[0].Attributes.Add("bgcolor", "#006699");
38 tc[0].Text = "全部信息</th></tr><tr>";
39 tc.Add(new TableHeaderCell());
40 tc[1].Text = "编号";
41 tc[1].Attributes.Add("bgcolor", "DarkSeaGreen");
42 tc.Add(new TableHeaderCell());
43 tc[2].Attributes.Add("colspan", "2");
44 tc[2].Attributes.Add("bgcolor", "LightSteelBlue");
45 tc[2].Text = "基本信息";
46 tc.Add(new TableHeaderCell());
47 tc[3].Attributes.Add("colspan", "2");
48 tc[3].Attributes.Add("bgcolor", "DarkSeaGreen");
49 tc[3].Text = "联系方式";
50 tc.Add(new TableHeaderCell());
51 tc[4].Attributes.Add("bgcolor", "LightSteelBlue");
52 tc[4].Text = "福利</th>";
53
54 }
55 }
posted on 2011-03-13 16:37  梦里故乡  阅读(273)  评论(2编辑  收藏  举报