对DataGrid中的数据删除时,如果当前页中只有一个数据行时,删除后就会出错:
“无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。”我在绑定前加入了下面这个,无效。。
if(this.DataGridEdit.CurrentPageIndex<this.DataGridEdit.PageCount)
{
this.DataGridEdit.CurrentPageIndex=this.DataGridEdit.PageCount;
}
“无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。”我在绑定前加入了下面这个,无效。。
if(this.DataGridEdit.CurrentPageIndex<this.DataGridEdit.PageCount)
{
this.DataGridEdit.CurrentPageIndex=this.DataGridEdit.PageCount;
}
解决方案 »
- 百分请教System.Web.UI.WebControls.TextBox?
- 请问一下 System.Web.HttpContext.Current.Request和Request的区别
- 如何在asp.net打开一个.exe的应用程序(在线等)
- 嵌入FLASH
- 为什么打不开原来的项目?????????
- 請問在CS後台代碼中如何才能判斷要上載的文件是從哪個上載文本框選擇的?
- 高手请来指点页面传值的问题
- 求助各位大狭,不慎感激!!!
- 用fileupload控件上传图片时怎么添加水印图片?
- 如何作出asp.net?
- 急问个问题。
- .NET 2005 怎么样接收后台的DataSet对象,用AJAX.NET实现
要么就要在重新绑定之前,根据数据源记录的条数计算一下当前页是否超出索引,是则往前翻页~
{
int empID = (int)dgPagerDel.DataKeys[e.Item.ItemIndex];
string sqlCom = "delete Employees where EmployeeID="+empID.ToString();
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//定义命令对象
SqlCommand cmd = new SqlCommand(sqlCom,conn);
//打开数据连接
conn.Open();
try
{
//执行SQL命令
cmd.ExecuteNonQuery();
//取得当前页的索引
lastEditedPage = dgPagerDel.CurrentPageIndex;
//如果有多页并且当前页中的项数仅有一项
if ((dgPagerDel.PageCount - dgPagerDel.CurrentPageIndex) == 1 && dgPagerDel.Items.Count == 1)
{
if (dgPagerDel.PageCount > 1)
{
lastEditedPage = lastEditedPage - 1;
}
else
{
lastEditedPage = 0;
}
}
dgPagerDel.CurrentPageIndex = lastEditedPage;
DataGridDataBind();
}
catch(SqlException err)
{
//输出异常信息
Response.Write(err.ToString());
}
finally
{
//关闭连接对象
conn.Close();
}
}
我的建议是
删除的时候判断
if(DataGrid.Items.Count==1)
{
if(DataGrid.CurrentPageIndex>0)
{
DataGrid.CurrentPageIndex--;
}
}这样可以转到上一页
{
if(DataGridEdit.CurrentPageIndex!=0)
DataGridEdit.CurrentPageIndex = DataGridEdit.CurrentPageIndex-1;
}
{
int AT_ID=Convert.ToInt16(this.DataGridEdit.DataKeys[e.Item.ItemIndex]);
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
SqlCommand cmdDel=new SqlCommand("Delete from AT_News where AT_ID="+AT_ID,con);
con.Open();
try
{
//执行SQL命令
cmdDel.ExecuteNonQuery();
//取得当前页的索引
int lastEditedPage = this.DataGridEdit.CurrentPageIndex;
//如果有多页并且当前页中的项数仅有一项
if ((this.DataGridEdit.PageCount - this.DataGridEdit.CurrentPageIndex) == 1 && this.DataGridEdit.Items.Count == 1)
{
if (this.DataGridEdit.PageCount > 1)
{
lastEditedPage = lastEditedPage - 1;
}
else
{
lastEditedPage = 0;
}
}
this.DataGridEdit.CurrentPageIndex = lastEditedPage;
articleBind(); }
catch(SqlException err)
{
//输出异常信息
Response.Write(err.ToString()); }
finally
{
con.Close();
}
}终于搞定了。谢谢各位。。