Copy Code code as follows:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
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;
public partial class Demo19:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (Page.IsPostBack = = False)
{
Binddata ();
}
}
public void Binddata ()
{
String strSQL = "Select Userid,c_name,e_name,updatadate,qq from Demo_user";
DataTable dt = SqlHelper.ExecuteDataset (sqlhelper.conn_string, CommandType.Text, strSQL, null). Tables[0];
Gridview.datasource = DT;
Gridview.datakeynames = new string[] {"UserID"};//primary key
Gridview.databind ();
}
protected void Gridview_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Gridview.pageindex = E.newpageindex;
Binddata ();
}
protected void Gridview_rowdatabound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
When the mouse passes, the row background color changes
E.row.attributes.add ("onmouseover", "This.style.backgroundcolor= ' #00A9FF");
The row background color changes when the mouse is moved
E.row.attributes.add ("onmouseout", "This.style.backgroundcolor= ' #FFFFFF");
}
}
}
<table align= "center" bgcolor= "#c0de98" border= "0" cellpadding= "0" cellspacing= "1" width= "99%" >
<tr>
<th colspan= "2" >
GridView Demo </th>
</tr>
<tr>
<TD colspan= "2" style= "width:100%;" >
<asp:gridview id= "GridView" runat= "Server" width= "100%" autogeneratecolumns= "False" allowpaging= "True" onpageindexchanging= "gridview_pageindexchanging" pagesize= "onrowdatabound=" Gridview_rowdatabound ">
<Columns>
<asp:boundfield datafield= "UserID" headertext= "UserID"/>
<asp:boundfield datafield= "C_name" headertext= "Chinese name"/>
<asp:boundfield datafield= "E_name" headertext= "English name"/>
<asp:boundfield datafield= "QQ" headertext= "QQ"/>
<asp:boundfield datafield= "updatadate" headertext= "Update Time"/>
</Columns>
<rowstyle horizontalalign= "Center"/>
<pagerstyle horizontalalign= "right"/>
</asp:GridView>
</td>
</tr>
</table>