转载自/UploadFile/9aea29/4111/
Step 1. Create new projectin visual studio.
Step 2. Open default.aspx and drag and drop GridView controlin design view.
Step 3. Open default.aspx.cs and add following line in usingdirectives:
usingSystem.Data.SqlClient;
Step 4. Open Web.config and add following line just above<system.web>:
<connectionStrings>
<addname="con"connectionString="DataSource=.\sqlexpress;initial catalog=database_name;integratedsecurity=true;"providerName="System.Data.SqlClient"/>
</connectionStrings>
Step 5. Open properties of GridView, change AllowSorting andAllowPaging property to True.
Step 6. Open default.aspx.cs and add following lines:
SqlConnectionconn =new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataView dv = new DataView();
dv = binddata();
GridView1.DataSource = dv;
GridView1.DataBind();
}
private DataView binddata()
{
conn.Open();
SqlDataAdapteradp = new SqlDataAdapter("select data from table", conn);
adp.Fill(ds);
if (ViewState["sortexp"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortexp"];
}
else
dv = ds.Tables[0].DefaultView;
conn.Close();
return dv;
}
protected void paging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = binddata();
GridView1.DataBind();
}
protected void sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortexp"] =e.SortExpression;
GridView1.DataSource = binddata();
GridView1.DataBind();
}
}
Step 7. Open properties of GridView and click on events,change PageIndexChanging to paging and Sorting to sorting.
Step 8. Build the project.