1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Demo19 : System.Web.UI.Page
13 {
14 protected void Page_Load( object sender, EventArgs e)
15 {
16 if (Page.IsPostBack == false )
17 {
18 BindData();
19 }
20 }
21
22 public void BindData()
23 {
24 string strSql = " select UserID,C_Name,E_Name,UpdataDate,QQ from Demo_User " ;
25 DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null ).Tables[ 0 ];
26
27 GridView.DataSource = dt;
28 GridView.DataKeyNames = new string [] { " UserID " }; // 主键
29 GridView.DataBind();
30 }
31
32 protected void GridView_PageIndexChanging( object sender, GridViewPageEventArgs e)
33 {
34 GridView.PageIndex = e.NewPageIndex;
35 BindData();
36 }
37
38 protected void GridView_RowDataBound( object sender, GridViewRowEventArgs e)
39 {
40 if (e.Row.RowType == DataControlRowType.DataRow)
41 {
42 // 鼠标经过时,行背景色变
43 e.Row.Attributes.Add( " onmouseover " , " this.style.backgroundColor='#00A9FF' " );
44 // 鼠标移出时,行背景色变
45 e.Row.Attributes.Add( " onmouseout " , " this.style.backgroundColor='#FFFFFF' " );
46 }
47 }
48 }
1 < table align = " center " bgcolor = " #c0de98 " border = " 0 " cellpadding = " 0 " cellspacing = " 1 " width = " 99% " >
2 < tr >
3 < th colspan = " 2 " >
4 GridView演示 th >
5 tr >
6 < tr >
7 < td colspan = " 2 " style = " width: 100%; " >
8 < asp:GridView ID = " GridView " runat = " server " Width = " 100% " AutoGenerateColumns = " False " AllowPaging = " True " OnPageIndexChanging = " GridView_PageIndexChanging " PageSize = " 12 " OnRowDataBound = " GridView_RowDataBound " >
9 < Columns >
10 < asp:BoundField DataField = " UserID " HeaderText = " UserID " />
11 < asp:BoundField DataField = " C_Name " HeaderText = " 中文名字 " />
12 < asp:BoundField DataField = " E_Name " HeaderText = " 英文名字 " />
13 < asp:BoundField DataField = " QQ " HeaderText = " QQ " />
14 < asp:BoundField DataField = " UpdataDate " HeaderText = " 更新时间 " />
15 Columns >
16 < RowStyle HorizontalAlign = " Center " />
17 < PagerStyle HorizontalAlign = " Right " />
18 asp:GridView >
19 td >
20 tr >
21 table >
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Demo19 : System.Web.UI.Page
13 {
14 protected void Page_Load( object sender, EventArgs e)
15 {
16 if (Page.IsPostBack == false )
17 {
18 BindData();
19 }
20 }
21
22 public void BindData()
23 {
24 string strSql = " select UserID,C_Name,E_Name,UpdataDate,QQ from Demo_User " ;
25 DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null ).Tables[ 0 ];
26
27 GridView.DataSource = dt;
28 GridView.DataKeyNames = new string [] { " UserID " }; // 主键
29 GridView.DataBind();
30 }
31
32 protected void GridView_PageIndexChanging( object sender, GridViewPageEventArgs e)
33 {
34 GridView.PageIndex = e.NewPageIndex;
35 BindData();
36 }
37
38 protected void GridView_RowDataBound( object sender, GridViewRowEventArgs e)
39 {
40 if (e.Row.RowType == DataControlRowType.DataRow)
41 {
42 // 鼠标经过时,行背景色变
43 e.Row.Attributes.Add( " onmouseover " , " this.style.backgroundColor='#00A9FF' " );
44 // 鼠标移出时,行背景色变
45 e.Row.Attributes.Add( " onmouseout " , " this.style.backgroundColor='#FFFFFF' " );
46 }
47 }
48 }
1 < table align = " center " bgcolor = " #c0de98 " border = " 0 " cellpadding = " 0 " cellspacing = " 1 " width = " 99% " >
2 < tr >
3 < th colspan = " 2 " >
4 GridView演示 th >
5 tr >
6 < tr >
7 < td colspan = " 2 " style = " width: 100%; " >
8 < asp:GridView ID = " GridView " runat = " server " Width = " 100% " AutoGenerateColumns = " False " AllowPaging = " True " OnPageIndexChanging = " GridView_PageIndexChanging " PageSize = " 12 " OnRowDataBound = " GridView_RowDataBound " >
9 < Columns >
10 < asp:BoundField DataField = " UserID " HeaderText = " UserID " />
11 < asp:BoundField DataField = " C_Name " HeaderText = " 中文名字 " />
12 < asp:BoundField DataField = " E_Name " HeaderText = " 英文名字 " />
13 < asp:BoundField DataField = " QQ " HeaderText = " QQ " />
14 < asp:BoundField DataField = " UpdataDate " HeaderText = " 更新时间 " />
15 Columns >
16 < RowStyle HorizontalAlign = " Center " />
17 < PagerStyle HorizontalAlign = " Right " />
18 asp:GridView >
19 td >
20 tr >
21 table >
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-621635/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-621635/