CopyCode The Code is 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)
{
// The row background color changes when the cursor passes
E. Row. Attributes. Add ("onmouseover", "This. style. backgroundcolor = '# 00a9ff '");
// The row background color changes when the cursor is removed.
E. Row. Attributes. Add ("onmouseout", "This. style. backgroundcolor = '# ffff '");
}
}
}
<Table align = "center" bgcolor = "# c0de98" border = "0" cellpadding = "0" cellspacing = "1" width = "99%">
<Tr>
<TH colspan = "2">
Gridview demonstration </Th>
</Tr>
<Tr>
<TD colspan = "2" style = "width: 100%;">
<Asp: gridview id = "gridview" runat = "server" width = "100%" autogeneratecolumns = "false" allowpaging = "true" onpageindexchanging = "inline" pagesize = "12" onrowdatabound = "gridview_rowdatabound ">
<Columns>
<Asp: boundfield datafield = "userid" headertext = "userid"/>
<Asp: boundfield datafield = "c_name" headertext = "文" "/>
<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>