数据有很多页,比如我输入2,应该转到第2页去,然后确是出现这个错误,一直没找到解决办法,
还望高手们多多赐教指定的参数已超出有效值的范围。参数名: value
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。参数名: value源错误:
行 205: public int PageIndex
行 206: {
行 207: get{return this.DataGrid1.CurrentPageIndex;}
行 208: set{this.DataGrid1.CurrentPageIndex = value;}
行 209: }源代码如下
private static DataSet GetCustomersData(int pageIndex,int pageSize,ref int recordCount,ref int pageCount)
{
string connString ="data source=127.0.0.1;initial catalog=Northwind;persist security info=False;user id=sa;password=";
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("GetCustomersDataPage",conn); comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));
comm.Parameters[0].Value = pageIndex; comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));
comm.Parameters[1].Value = pageSize; comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));
comm.Parameters[2].Direction = ParameterDirection.Output; comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));
comm.Parameters[3].Direction = ParameterDirection.Output; comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds); recordCount = (int)comm.Parameters[2].Value;
pageCount = (int)comm.Parameters[3].Value; return ds;
}
//绑定数据到DataGrid,同时刷新数据总记录数
private void DataGridDataBind()
{
DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);
this.DataGrid1.VirtualItemCount = RecordCount;
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
if(RecordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页
this.LtlPageCount.Text = "0";
else
this.LtlPageCount.Text = PageCount.ToString();
if(RecordCount == 0)
this.LtlPageIndex.Text = "0";
else
this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有页数的情况下前台显示页数加1
this.LtlPageSize.Text = PageSize.ToString();
this.LtlRecordCount.Text = RecordCount.ToString(); }
// 注册DataGrid分页事件
//分页事件处理
public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid dg = (DataGrid)source;
dg.CurrentPageIndex = e.NewPageIndex;
DataGridDataBind();
}
// 最好判断当前页面是否是第一次加载,防止重复加载两次数据,
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
DataGridDataBind();
}
ViewState["currentpage"]=PageIndex;
}
public void FirstPage_Click(object sender, System.EventArgs e)
{
//disabled首页按钮和上一页按钮
PageIndex=0;
FirstPage.Enabled = false;
PrevPage.Enabled = false;
ViewState["currentpage"] = PageIndex;
DataGridDataBind();
//如果不止一页
if(RecordCount>((int)ViewState["currentpage"]+1)*PageSize)
{
NextPage.Enabled = true;
}
if(RecordCount>((int)ViewState["currentpage"]+1)*PageSize)
{
LastPage.Enabled = true;
}
} public void PrevPage_Click(object sender, System.EventArgs e)
{ ViewState["currentpage"] = (int)ViewState["currentpage"]-1;
PageIndex=(int)ViewState["currentpage"];
DataGridDataBind();
NextPage.Enabled = true;
LastPage.Enabled = true;
//如果到首页则disabled首页和上一页按钮
if((int)ViewState["currentpage"]==0)
{
PrevPage.Enabled = false;
FirstPage.Enabled = false;
}
}
public void NextPage_Click(object sender, System.EventArgs e)
{
ViewState["currentpage"] = (int)ViewState["currentpage"]+1;
PageIndex= (int)ViewState["currentpage"];
DataGridDataBind();
PrevPage.Enabled = true;
FirstPage.Enabled = true;
//如果已经到了最后一页
if(((int)ViewState["currentpage"]+1)*PageSize>RecordCount)
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
} //末页
public void LastPage_Click(object sender, System.EventArgs e)
{
ViewState["currentpage"] = (int)Math.Ceiling(RecordCount/PageSize);
PageIndex= (int)ViewState["currentpage"];
this.Label1.Text=(Math.Ceiling(RecordCount/PageSize)).ToString();
DataGridDataBind();
LastPage.Enabled = false;
NextPage.Enabled = false; //如果有不止一页的纪录
if((int)ViewState["currentpage"]>1)
{
FirstPage.Enabled = true;
PrevPage.Enabled = true;
}
//如果只有一页的纪录
else
{
FirstPage.Enabled = false;
PrevPage.Enabled = false;
}
}
//跳转
private void NewPage_Go(string i)
{
PageIndex = Int32.Parse(i);
if (PageIndex<=0)
{
PageIndex = 0;
ViewState["currentpage"] = PageIndex;
PageIndex=(int)ViewState["currentpage"];
}
else
{
if((RecordCount%PageSize)>0)
{
PageIndex=(int)(RecordCount/PageSize)-2;
ViewState["currentpage"] = PageIndex;
PageIndex=(int)ViewState["currentpage"];
}
else
{
PageIndex=(int)(RecordCount/PageSize)-1;
}
} this.Label1.Text=PageIndex.ToString();
//简单起见,将所有的linkbutton全部改为enable=true
FirstPage.Enabled = true;
NextPage.Enabled = true;
LastPage.Enabled = true;
PrevPage.Enabled = true;
DataGridDataBind();
ViewState["currentpage"] = PageIndex;
} public void NewPageGo_Click(object sender, System.EventArgs e)
{
NewPage_Go(NewPageIndex.Text.Trim());
} public int PageCount
{
get{return pageCount;}
}
public int PageSize
{
get{return this.DataGrid1.PageSize;}
} public int PageIndex
{
get{return this.DataGrid1.CurrentPageIndex;}
set{this.DataGrid1.CurrentPageIndex = value;}
} public int RecordCount
{
get{return recordCount;}
}
}
还望高手们多多赐教指定的参数已超出有效值的范围。参数名: value
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。参数名: value源错误:
行 205: public int PageIndex
行 206: {
行 207: get{return this.DataGrid1.CurrentPageIndex;}
行 208: set{this.DataGrid1.CurrentPageIndex = value;}
行 209: }源代码如下
private static DataSet GetCustomersData(int pageIndex,int pageSize,ref int recordCount,ref int pageCount)
{
string connString ="data source=127.0.0.1;initial catalog=Northwind;persist security info=False;user id=sa;password=";
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("GetCustomersDataPage",conn); comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));
comm.Parameters[0].Value = pageIndex; comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));
comm.Parameters[1].Value = pageSize; comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));
comm.Parameters[2].Direction = ParameterDirection.Output; comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));
comm.Parameters[3].Direction = ParameterDirection.Output; comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds); recordCount = (int)comm.Parameters[2].Value;
pageCount = (int)comm.Parameters[3].Value; return ds;
}
//绑定数据到DataGrid,同时刷新数据总记录数
private void DataGridDataBind()
{
DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);
this.DataGrid1.VirtualItemCount = RecordCount;
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
if(RecordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页
this.LtlPageCount.Text = "0";
else
this.LtlPageCount.Text = PageCount.ToString();
if(RecordCount == 0)
this.LtlPageIndex.Text = "0";
else
this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有页数的情况下前台显示页数加1
this.LtlPageSize.Text = PageSize.ToString();
this.LtlRecordCount.Text = RecordCount.ToString(); }
// 注册DataGrid分页事件
//分页事件处理
public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid dg = (DataGrid)source;
dg.CurrentPageIndex = e.NewPageIndex;
DataGridDataBind();
}
// 最好判断当前页面是否是第一次加载,防止重复加载两次数据,
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
DataGridDataBind();
}
ViewState["currentpage"]=PageIndex;
}
public void FirstPage_Click(object sender, System.EventArgs e)
{
//disabled首页按钮和上一页按钮
PageIndex=0;
FirstPage.Enabled = false;
PrevPage.Enabled = false;
ViewState["currentpage"] = PageIndex;
DataGridDataBind();
//如果不止一页
if(RecordCount>((int)ViewState["currentpage"]+1)*PageSize)
{
NextPage.Enabled = true;
}
if(RecordCount>((int)ViewState["currentpage"]+1)*PageSize)
{
LastPage.Enabled = true;
}
} public void PrevPage_Click(object sender, System.EventArgs e)
{ ViewState["currentpage"] = (int)ViewState["currentpage"]-1;
PageIndex=(int)ViewState["currentpage"];
DataGridDataBind();
NextPage.Enabled = true;
LastPage.Enabled = true;
//如果到首页则disabled首页和上一页按钮
if((int)ViewState["currentpage"]==0)
{
PrevPage.Enabled = false;
FirstPage.Enabled = false;
}
}
public void NextPage_Click(object sender, System.EventArgs e)
{
ViewState["currentpage"] = (int)ViewState["currentpage"]+1;
PageIndex= (int)ViewState["currentpage"];
DataGridDataBind();
PrevPage.Enabled = true;
FirstPage.Enabled = true;
//如果已经到了最后一页
if(((int)ViewState["currentpage"]+1)*PageSize>RecordCount)
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
} //末页
public void LastPage_Click(object sender, System.EventArgs e)
{
ViewState["currentpage"] = (int)Math.Ceiling(RecordCount/PageSize);
PageIndex= (int)ViewState["currentpage"];
this.Label1.Text=(Math.Ceiling(RecordCount/PageSize)).ToString();
DataGridDataBind();
LastPage.Enabled = false;
NextPage.Enabled = false; //如果有不止一页的纪录
if((int)ViewState["currentpage"]>1)
{
FirstPage.Enabled = true;
PrevPage.Enabled = true;
}
//如果只有一页的纪录
else
{
FirstPage.Enabled = false;
PrevPage.Enabled = false;
}
}
//跳转
private void NewPage_Go(string i)
{
PageIndex = Int32.Parse(i);
if (PageIndex<=0)
{
PageIndex = 0;
ViewState["currentpage"] = PageIndex;
PageIndex=(int)ViewState["currentpage"];
}
else
{
if((RecordCount%PageSize)>0)
{
PageIndex=(int)(RecordCount/PageSize)-2;
ViewState["currentpage"] = PageIndex;
PageIndex=(int)ViewState["currentpage"];
}
else
{
PageIndex=(int)(RecordCount/PageSize)-1;
}
} this.Label1.Text=PageIndex.ToString();
//简单起见,将所有的linkbutton全部改为enable=true
FirstPage.Enabled = true;
NextPage.Enabled = true;
LastPage.Enabled = true;
PrevPage.Enabled = true;
DataGridDataBind();
ViewState["currentpage"] = PageIndex;
} public void NewPageGo_Click(object sender, System.EventArgs e)
{
NewPage_Go(NewPageIndex.Text.Trim());
} public int PageCount
{
get{return pageCount;}
}
public int PageSize
{
get{return this.DataGrid1.PageSize;}
} public int PageIndex
{
get{return this.DataGrid1.CurrentPageIndex;}
set{this.DataGrid1.CurrentPageIndex = value;}
} public int RecordCount
{
get{return recordCount;}
}
}
public static DataTable ExecuteDatatable(SqlConnection connection, CommandType commandType, string commandText, SqlParameter[] commandParameters,object[] Parmetersvalues,int start ,int size)
{
System.Data .SqlClient .SqlCommand sqlcomm=new SqlCommand ();
PrepareCommand(sqlcomm,connection,commandType,commandText,commandParameters);
AssignParameterValues(commandParameters,Parmetersvalues);
System.Data .SqlClient .SqlDataAdapter sqldata=new SqlDataAdapter();
sqldata.SelectCommand=sqlcomm;
System.Data .DataTable dt=new DataTable ("table");
sqldata.Fill (dt);
System.Data .DataSet ds=new DataSet ();
sqldata.Fill (ds,start,size,"table");
sqlcomm.Parameters .Clear ();
return ds.Tables[0];
}注意 :sqldata.Fill (ds,start,size,"table");再查查 msdn
用这个返回也差不多呵...
start 起始行.
size 从起始后的行数