Gridview. aspx
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "gridview. aspx. cs" inherits = "gridview" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: gridview id = "gridview1" runat = "server" allowpaging = "true" allowsorting = "true"
Autogeneratecolumns = "false" datakeynames = "customerid" datasourceid = "sqldatasource1"
Onrowcreated = "gridview1_rowcreated" onsorting = "gridview1_sorting" pagesize = "5">
<Columns>
<Asp: boundfield datafield = "companyName" headertext = "company name" sortexpression = "companyName"/>
<Asp: boundfield datafield = "contactname" headertext = "Contact" sortexpression = "contactname"/>
<Asp: boundfield datafield = "Address" headertext = "Address" sortexpression = "Address"/>
<Asp: boundfield datafield = "city" headertext = "city" sortexpression = "city"/>
</Columns>
</ASP: gridview>
<Asp: sqldatasource id = "sqldatasource1" runat = "server" connectionstring = "<% $ connectionstrings: northwindconnectionstring %>"
Selectcommand = "select * from [MERs]"> </ASP: sqldatasource>
<Asp: Label id = "label1" runat = "server" text = "label"> </ASP: Label> <br/>
<Br/>
D </div>
</Form>
</Body>
</Html>
Gridview. aspx. csusing 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 gridview: system. Web. UI. Page
{
String sortexp = ""; // sort expression
String sortdir = ""; // sorting direction
Protected void page_load (Object sender, eventargs E)
{
}
Protected void gridview1_rowcreated (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. header)
{
Foreach (tablecell TC in E. Row. cells)
{
// This method adds a new control directly, or you can change it to an image.
// Label ls = new label ();
// Ls. Font. Name = "webdings ";
// Ls. Text = "5 ";
// TC. Controls. Add (LS );
If (TC. Controls. Count> 0) // check whether a linkbutton is generated.
String S1 = (linkbutton) TC. controls [0]). text;
(Linkbutton) TC. controls [0]). Text = s1.replace (S1, S1 + "<font face = 'webline'> 5 </font> ");
If (TC. Controls. Count> 0 & TC. controls [0]. GetType (). tostring () = "system. Web. UI. webcontrols. datacontrollinkbutton ")
{
If (linkbutton) TC. controls [0]). commandargument = sortexp)
{
// Label L = new label ();
// L. Font. Name = "webdings ";
// If (sortdir = "ASCENDING ")
//{
// L. Text = "5 ";
//}
// Else
//{
// L. Text = "6 ";
//}
// TC. Controls. Remove (LS );
// TC. Controls. Add (L );
String S2 = (linkbutton) TC. controls [0]). text;
If (sortdir = "descending ")
{
(Linkbutton) TC. controls [0]). Text = s2.replace ("5", "6 ");
}
}
}
}
}
}
}
Protected void gridviewinclusorting (Object sender, gridviewsorteventargs E)
{
Sortexp = E. sortexpression. tostring ();
Sortdir = E. sortdirection. tostring ();
}
}