GridView有自带的分页功能,但是今天我用的时候出错了。
报错:The data source does not support server-side data paging。
后来网上查到SqlDataReader 不支持分页,需要用SqlDataAdapter 和DataSet 代替,代码如下。
public DataSet GetAllUserInfo()
{
SqlConnection myConnection = new SqlConnection(
ConfigurationManager.ConnectionStrings["EmergencyHandlingDBConnectionString"].ConnectionString);
SqlCommand myCommand = myConnection.CreateCommand();//创建command
myCommand.CommandType = CommandType.StoredProcedure;//指定执行存储过程操作
myCommand.CommandText = "GetUserInfo";//存储过程名称
//定义DateReader
//SqlDataReader dr = null;
SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds);
try
{
//myConnection.Open();
//读取数据
//dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
return ds;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
DataSet ds = user.GetAllUserInfo();
//SqlDataReader dr = user.GetAllUserInfo();
UserManageGridView.DataSource = ds;
UserManageGridView.DataBind();
//dr.Close();
}
然后代替之后没有上面报错了,但是翻页的时候又报错了...
报错:The GridView 'UserManageGridView' fired event PageIndexChanging which wasn't handled.
再查,发现是没有处理PageIndexChanging时间,然后加上下面代码,终于能正常运行。
protected void UserManageGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
UserManageGridView.PageIndex = e.NewPageIndex;
BindGrid();
}
需要学习的如此之多啊,对于新手再小的问题都是大问题...哎!
路漫漫啊......