Code
protected void Button1_Click(object sender, EventArgs e)
{
int startIndex =1;
int pageSize = GridView1.PageSize;
BindGridView(startIndex,pageSize);
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int startIndex =e.NewPageIndex + 1;
int pageSize = GridView1.PageSize;
BindGridView(startIndex, pageSize);
}
{
ObjectDataSource1.EnablePaging = true;
//调用的类名
ObjectDataSource1.TypeName = "Class1";
ObjectDataSource1.SelectMethod = "GetGridViewData";
ObjectDataSource1.SelectCountMethod = "GetRecordsCount";
ObjectDataSource1.DataObjectTypeName = "System.Data.DataSet";
ObjectDataSource1.StartRowIndexParameterName = "startIndex";
ObjectDataSource1.MaximumRowsParameterName = "pageSize";
//ObjectDataSource1.SortParameterName = "orderColumn";
GridView1.DataSourceID = ObjectDataSource1.ID;
}
protected void Button1_Click(object sender, EventArgs e)
{
int startIndex =1;
int pageSize = GridView1.PageSize;
BindGridView(startIndex,pageSize);
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
int startIndex =e.NewPageIndex + 1;
int pageSize = GridView1.PageSize;
BindGridView(startIndex, pageSize);
}
private void BindGridView(int startIndex, int pageSize)
{
ObjectDataSource1.EnablePaging = true;
//调用的类名
ObjectDataSource1.TypeName = "Class1";
ObjectDataSource1.SelectMethod = "GetGridViewData";
ObjectDataSource1.SelectCountMethod = "GetRecordsCount";
ObjectDataSource1.DataObjectTypeName = "System.Data.DataSet";
ObjectDataSource1.StartRowIndexParameterName = "startIndex";
ObjectDataSource1.MaximumRowsParameterName = "pageSize";
//ObjectDataSource1.SortParameterName = "orderColumn";
GridView1.DataSourceID = ObjectDataSource1.ID;
}
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; using System.Data.SqlClient;
using System.Text; /// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public static DataSet GetGridViewData(int startIndex, int pageSize)
{
//当前页
int endIndex = startIndex+ pageSize + 1;
StringBuilder sb = new StringBuilder();
sb.Append("With[sqlRowNumber]as");
sb.Append("(select row_number() over(order by id asc) as RowId,* from NameList)");
sb.Append("select * from [sqlRowNumber] where RowId between ").Append(startIndex+1);
sb.Append(" and ").Append(endIndex);
string sql = sb.ToString();
return Query(sql);
{
StringBuilder sb = new StringBuilder();
sb.Append("select id from namelist");
string sql = sb.ToString();
int count = Query(sql).Tables[0].Rows.Count;
return count;
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
string connectionString = ConfigurationManager.ConnectionStrings["SqlDB"].ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
return ds;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; using System.Data.SqlClient;
using System.Text; /// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public static DataSet GetGridViewData(int startIndex, int pageSize)
{
//当前页
int endIndex = startIndex+ pageSize + 1;
StringBuilder sb = new StringBuilder();
sb.Append("With[sqlRowNumber]as");
sb.Append("(select row_number() over(order by id asc) as RowId,* from NameList)");
sb.Append("select * from [sqlRowNumber] where RowId between ").Append(startIndex+1);
sb.Append(" and ").Append(endIndex);
string sql = sb.ToString();
return Query(sql);
}
//GridView显示数据的总数
{
StringBuilder sb = new StringBuilder();
sb.Append("select id from namelist");
string sql = sb.ToString();
int count = Query(sql).Tables[0].Rows.Count;
return count;
}
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
string connectionString = ConfigurationManager.ConnectionStrings["SqlDB"].ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
return ds;
}
}
}