Gridview noneCodeSort by PAGE
Select, edit, cancel, and delete a gridview
Positive and Negative order of gridview
The combination of gridview and dropdownlist
Combination of gridview and checkbox
Method 1
Method 2: change the background color of a row in the gridview
The confirmation dialog box is displayed when you delete a gridview.
Auto-numbered by gridview
Use gridview to customize string formats such as time currency
Use "..." to replace the ultra-long string in the gridview implementation
General line feed and forced line feed of gridview
Hide a column in the gridview
A new page appears in the gridview or a new window appears.
Gridview Fixed Header (CSS is not required for Javascript, 2 lines of code, very easy to use)
Perfect version for merging multiple headers in the gridview (for example, merging three columns and three rows)
The gridview highlights a cell (for example, the value is less than the number of cells and the score fails)
Add auto-sum to the gridview to calculate the average Subtotal
The data imported from the gridview into the Excel/Excel file is read into the gridview.
1. The gridview does not have code to sort by PAGE:
:
1. Set allowsorting to true, and the aspx code is allowsorting = "true ";
2. By default, there are 10 entries on one page. to modify the number of entries on each page, modify the pagesize. In the aspx code, it is pagesize = "12 ".
3. The default value is unidirectional sorting. Right-click the "properties" in the gridview and select allowsorting as true.
2. Select, edit, cancel, and delete the gridview:
:
Background code:
You can use sqlhelper, Which is useless in this article. The Code is as follows:
Using system;
Using system. Data;
Using system. configuration;
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;
Using system. Data. sqlclient;
Public partial class _ default: system. Web. UI. Page
{
// Clear moon http://blog.csdn.net/21aspnet
Sqlconnection sqlcon;
Sqlcommand sqlcom;
String strcon = "Data Source = (local); database = database name; uid = Account; Pwd = password ";
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
BIND ();
}
}
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)
{
Gridview1.editindex = E. neweditindex;
BIND ();
}
// Delete
Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
String sqlstr = "delete from table where id = '" + gridview1.datakeys [E. rowindex]. value. tostring () + "'";
Sqlcon = new sqlconnection (strcon );
Sqlcom = new sqlcommand (sqlstr, sqlcon );
Sqlcon. open ();
Sqlcom. executenonquery ();
Sqlcon. Close ();
BIND ();
}
// Update
Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
Sqlcon = new sqlconnection (strcon );
String sqlstr = "Update table set field 1 = '"
+ (Textbox) (gridview1.rows [E. rowindex]. cells [1]. controls [0]). text. tostring (). trim () + "', Field 2 = '"
+ (Textbox) (gridview1.rows [E. rowindex]. cells [2]. controls [0]). text. tostring (). trim () + "', Field 3 = '"
+ (Textbox) (gridview1.rows [E. rowindex]. cells [3]. controls [0]). text. tostring (). trim () + "'where id = '"
+ Gridview1.datakeys [E. rowindex]. value. tostring () + "'";
Sqlcom = new sqlcommand (sqlstr, sqlcon );
Sqlcon. open ();
Sqlcom. executenonquery ();
Sqlcon. Close ();
Gridview1.editindex =-1;
BIND ();
}
// Cancel
Protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs E)
{
Gridview1.editindex =-1;
BIND ();
}
// Bind
Public void BIND ()
{
String sqlstr = "select * from table ";
Sqlcon = new sqlconnection (strcon );
Sqldataadapter myda = new sqldataadapter (sqlstr, sqlcon );
Dataset myds = new dataset ();
Sqlcon. open ();
Myda. Fill (myds, "table ");
Gridview1.datasource = myds;
Gridview1.datakeynames = new string [] {"ID"}; // primary key
Gridview1.databind ();
Sqlcon. Close ();
}
}
Main front-end code:
......
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" cellpadding = "4"
Forecolor = "#333333" gridlines = "NONE" onrowdeleting = "gridview1_rowdeleting" onrowediting = "gridview1_rowediting"
Onrowupdating = "gridview1_rowupdating" onrowcancelingedit = "gridview1_rowcancelingedit">
<Footerstyle backcolor = "#990000" font-bold = "true" forecolor = "white"/>
<Columns>
<Asp: boundfield datafield = "ID card number" headertext = "User ID" readonly = "true"/>
<Asp: boundfield datafield = "name" headertext = "User Name"/>
<Asp: boundfield datafield = "employee gender" headertext = "gender"/>
<Asp: boundfield datafield = "Home Address" headertext = "Home Address"/>
<Asp: commandfield headertext = "select" showselectbutton = "true"/>
<Asp: commandfield headertext = "edit" showeditbutton = "true"/>
<Asp: commandfield headertext = "delete" showdeletebutton = "true"/>
</Columns>
<Rowstyle forecolor = "#000066" type = "regxph" text = "yourobjectname"/>
<Selectedrowstyle backcolor = "#669999" font-bold = "true" forecolor = "white"/>
<Pagerstyle backcolor = "white" forecolor = "#000066" horizontalalign = "Left"/>
<Headerstyle backcolor = "#006699" font-bold = "true" forecolor = "white"/>
</ASP: gridview>
3. Positive and Negative order of the gridview:
: The order of the vertex name is two times, and the order of the vertex name is the same as that of the other vertex.
Background code:
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;
Using system. Data. sqlclient;
Public partial class default3: system. Web. UI. Page
{
// Clear moon's blog http://blog.csdn.net/21aspnet
Sqlconnection sqlcon;
String strcon = "Data Source = (local); database = beifeng trade; uid = sa; Pwd = ";
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Viewstate ["sortorder"] = "ID card number ";
Viewstate ["orderdire"] = "ASC ";
BIND ();
}
}
Protected void gridviewinclusorting (Object sender, gridviewsorteventargs E)
{
String spage = E. sortexpression;
If (viewstate ["sortorder"]. tostring () = spage)
{
If (viewstate ["orderdire"]. tostring () = "DESC ")
Viewstate ["orderdire"] = "ASC ";
Else
Viewstate ["orderdire"] = "DESC ";
}
Else
{
Viewstate ["sortorder"] = E. sortexpression;
}
BIND ();
}
Public void BIND ()
{
string sqlstr = "select top 5 * From feihu Studio";
sqlcon = new sqlconnection (strcon);
sqldataadapter myda = new sqldataadapter (sqlstr, sqlcon);
dataset myds = new dataset ();
sqlcon. open ();
myda. fill (myds, "feihu Studio");
dataview view = myds. tables ["feihu Studio"]. defaultview;
string sort = (string) viewstate ["sortorder"] + "" + (string) viewstate ["orderdire"];
View. sort = sort;
gridview1.datasource = view;
gridview1.databind ();
sqlcon. close ();
}< BR >}
main front-end code:
cellpadding = "3" font-size = "9pt" onsorting = "gridview#sorting "backcolor =" white "bordercolor =" # cccccc "borderstyle =" NONE "borderwidth =" 1px ">
</Columns>
<Rowstyle forecolor = "#000066" type = "regxph" text = "yourobjectname"/>
<Selectedrowstyle backcolor = "#669999" font-bold = "true" forecolor = "white"/>
<Pagerstyle backcolor = "white" forecolor = "#000066" horizontalalign = "Left"/>
<Headerstyle backcolor = "#006699" font-bold = "true" forecolor = "white"/>
</ASP: gridview>
4. Combine the gridview with the drop-down menu dropdownlist:
:
Background code:
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;
Using system. Data. sqlclient;
Public partial class default4: system. Web. UI. Page
{
Sqlconnection sqlcon;
String strcon = "Data Source = (local); database = beifeng trade; uid = sa; Pwd = sa ";
Protected void page_load (Object sender, eventargs E)
{
Dropdownlist DDL;
If (! Ispostback)
{
String sqlstr = "select top 5 * From feihu Studio ";
Sqlcon = new sqlconnection (strcon );
Sqldataadapter myda = new sqldataadapter (sqlstr, sqlcon );
Dataset myds = new dataset ();
Sqlcon. open ();
Myda. Fill (myds, "feihu Studio ");
Gridview1.datasource = myds;
Gridview1.databind ();
For (INT I = 0; I <= gridview1.rows. Count-1; I ++)
{
Datarowview mydrv = myds. Tables [""]. defaultview [I];
If (convert. tostring (mydrv ["employee gender"]). Trim () = "true ")
{
DDL = (dropdownlist) gridview1.rows [I]. findcontrol ("dropdownlist1 ");
DDL. selectedindex = 0;
}
If (convert. tostring (mydrv ["employee gender"]). Trim () = "false ")
{
DDL = (dropdownlist) gridview1.rows [I]. findcontrol ("dropdownlist1 ");
DDL. selectedindex = 1;
}
}
Sqlcon. Close ();
}
}
Public sqldatareader ddlbind ()
{
String sqlstr = "select distinct employee gender from feihu Studio ";
Sqlcon = new sqlconnection (strcon );
Sqlcommand sqlcom = new sqlcommand (sqlstr, sqlcon );
Sqlcon. open ();
Return sqlcom. executereader ();
}