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 );