请哪位高手帮我解决一下这个问题,
给我一个例子我学习学习,先谢了。
给我一个例子我学习学习,先谢了。
解决方案 »
- 急!在线等~~~3分钟倒计时问题!!!
- 关于GridView复选框的问题,快解决,快结帖,请大家帮帮忙!
- 刚上班两星期,新人多指教
- asp.net 怎么把程序部署到客户
- 添加Web引用时,出现“处的文档未被识别为已知的文档类型”的错误,怎么解决?
- GridView导出到Excel出现的几个问题
- 有谁知道CSDN的“?temp=.4661676”是干什么的?
- ASP编译的DLL文件是否可以反编译?
- 赖皮的帮忙,DROPGID与IMAGE的合用代码。
- treeview在浏览器中不能显示
- Compiler Error Message: BC30456: 'Culture' is not a member of 'ASP.personalattendanceview_aspx'.
- 如何用ASP.NET做一个留言板(新手~~~~~~~急救)
private void Delete()
{
//count为选中(要删除)的记录数
int count = 0;
//执行删除操作
for(int n=0;n<InterviewHistoryGrid.Items.Count;n++)
{
if(((CheckBox)myDataGrid.Items[n].FindControl("Check")).Checked)
{
//(这里执行你的删除操作)
...
//
//选中的记录加一
++ count;
}
}
//判断是否选中一整页数据 而且当前页非首页
if(count == myDataGrid.Items.Count && myDataGrid.CurrentPageIndex > 0 )
{
-- myDataGrid.CurrentPageIndex;
}
//绑定DataGrid
myDataGridBind();
}
{
//count为选中(要删除)的记录数
int count = 0;
//执行删除操作
for(int n=0;n<myDataGrid.Items.Count;n++)
{
if(((CheckBox)myDataGrid.Items[n].FindControl("Check")).Checked)
{
//(这里执行你的删除操作)
...
//
//选中的记录加一
++ count;
}
}
//判断是否选中一整页数据 而且当前页非首页
if(count == myDataGrid.Items.Count && myDataGrid.CurrentPageIndex > 0 )
{
-- myDataGrid.CurrentPageIndex;
}
//绑定DataGrid
myDataGridBind();
}
在论坛里见过不少网友问过这样的问题:当DataGrid翻到最后一页后,如果当前页的数据只有一条,那么删除这条记录就会出现“异常详细信息: System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。”这样的错误,出现这个错误的原因是当最后一页上的这条记录被删除并重新绑定数据之后,DataGrid的总页数减少了一页,而它的CurrentPageIndex属性值却仍然是删除记录前最后一页的页索引值,这个值超出了它的总页数。一些网友解决这个问题的方法是:在删除记录之后立即将DataGrid的CurrentPageIndex设为0,即跳回第一页,这个方法虽然可以避免再发生这样的错误,然而却很不灵活,因为如果用户要删除最后一页上的两条记录,那么在删除了其中一条后,不得不重新点击分页按钮进入最后一页来删除第二条,而且很多时候这种方法会给用户带来困扰,因为很容易把跳转到的第一页的数据当成是正执行删除命令的最后一页的数据,其实这个问题有更好的解决办法,如果我们用的是DataGrid的内建的删除功能,即一次只能删除一条记录的话,那么在它的DeleteCommand事件处理程序中,可以通过下面的方法,来实现当最后一页没有数据时,自动跳回前一页,否则只需要重新绑定数据即可:void DataGrid1_Delete(object src,DataGridCommandEventArgs e){
//执行删除命令,从数据库或其它保存数据的地方删除DataGrid中对应的记录(略)
//如果当前页上只有一条数据并且DataGrid的当前页索引大于0,那么就让DataGrid返回上一页
if(DataGrid1.Items.Count==1&&DataGrid1.CurrentPageIndex>0){
DataGrid1.CurrentPageIndex--;
}
//重新绑定数据
BindData();
}如果用了模板列中的CheckBox,实现一次删除多条选中的记录的话,那么可以在删除按钮的Click事件处理程序中这样来实现上面的功能:
void deleteButton_Click(object src,EventArgs e){
int deletedRows=0; //记录删除掉的行数
foreach(DataGridItem item in DataGrid1.Items){
if(item.ItemType==ListItemType.Item||item.ItemType==ListItemType.AlternatingItem){
CheckBox ckbox=item.FindControl("delckbox") as CheckBox;
if(ckbox!=null&&ckbox.Checked){
deletedRows++;
//从数据库或其它保存数据的地方删除DataGrid中对应的记录(略)
}
}
}
//如果当前页的记录总数等于已经被删除掉的记录数并且DataGrid当前页索引大于0,那么让DataGrid返回上一页
if(deletedRows==DataGrid1.Items.Count&&DataGrid.CurrentPageIndex>0){
DataGrid1.CurrentPageIndex--;
}
//重新绑定数据
BindData();
}
if(DataGrid1.Items.Coun==0&&DataGrid.CurrentPageIndex>0){
DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-2;
}
if(dg_Goods.PageCount-dg_Goods.CurrentPageIndex==1 && dg_Goods.Items.Count==1)
{
if(dg_Goods.PageCount>1)
{
dg_Goods.CurrentPageIndex=dg_Goods.CurrentPageIndex-1;
}
else
{
dg_Goods.CurrentPageIndex=0;
}
}
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string Sql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";楼主你所碰到的错误我曾经也碰到过
只要你加上一个if语句就OK了
如果你还没有解觉的话就加我QQ:274787137
http://nhlinkin.cnblogs.com/articles/125426.html