首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
tellmenow (22)
cutemouse (22)
softj (78)
iiprogram (69)
qdzx2008 (50)
goodboy1881 (14)
wangchinaking (58)
fancyhf (1)
harrymeng (41)
yjz0065 (113)
CSDN - 文档中心 - .NET 阅读:10994   评论: 10    参与评论
标题   DataGrid在分页状态下删除纪录的问题     选择自 hbzxf 的 Blog
关键字   DataGrid 分页 问题
出处  

在分页状态下删除纪录的问题

hbzxf (阿好)
http://www.cnblogs.com/hbzxf

        在使用DataGrid分页的时候,正常情况下,绑定数据库列表纪录时会自动产生分页的效果,然而我发觉在删除纪录的时候总会发生"无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。"的异常,其实解决这个问题很简单,我们要做的就是在DataGrid1_DeleteCommand事件中判断CurrentPageIndex的值,并根据不同的结果来绑定DataGrid。

 //检索数据库的函数
  public DataSet GetZcbd()
  {
   try
   {
    DataSet ds=new DataSet();   
    string searchString="select id,yy,bj from zc";
    da=new OleDbDataAdapter(searchString,conn);
    da.Fill(ds,"yy");    
    return ds;
   }
   catch
   {
    return null;    
   }  
           
  }

 //绑定DataGrid   
private void BindGrid()
  {
   DataSet ds = new DataSet();
   ds = us.GetZcbd();
   if (ds!=null)
   {
    this.DataGrid1.DataSource = ds;
    this.DataGrid1.DataBind();
   }
   else
   {
    msg.Alert("加载数据错误!",Page);
   }
  }

//删除数据库纪录函数
  public string DeleteZcbd(int bdID)
  {

   int count = this.IfExiseZysx(bdID);//不必理会次句,默认count=1
   if (count <= 0) return "false";
   else
   {
    string sqlStr = "delete from zcwhere id="+bdID;
    OleDbCommand cmd = new OleDbCommand(sqlStr,conn);

    conn.Open();

    try
    {
     cmd.ExecuteNonQuery();
     return "true";
    }
    catch(Exception e)
    {
     return e.Message.ToString();
    }
    finally
    {
     conn.Close();
    }      
   }
  }

// DataGrid1_DeleteCommand事件修改函数  
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   int bdID = int.Parse(DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString());
   string isDel = us.DeleteZcbd(bdID);
   int CurrentPage = 0;
   if (isDel == "true")
   {
    if(this.DataGrid1.CurrentPageIndex == this.DataGrid1.PageCount -1)
    {
   if (this.DataGrid1.CurrentPageIndex == 0)
    {
     this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount -1;
    }
    else
    {
     if (this.DataGrid1.Items.Count % this.DataGrid1.PageSize == 1)
     {
      CurrentPage = 2;
     }
     else
     {
      CurrentPage = 1;
     }
     this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount - CurrentPage;
    }
   }

    this.BindGrid();
   }
   else
   {
       msg.Alert("删除数据错误!",Page);
   }
 
  }
    注释:msg为一个类似WinForm的messagebox对话框,不必理会。可以使用label.Text代替

代码很乱,敬请谅解!

感谢我的好友小琳在此提供了技术支持,他是一位出色的软件工程师。

2004.4.1


相关文章
对该文的评论
CSDN 网友 ( 2004-08-19)
都他妈的是白痴!!!!!!!!!
CSDN 网友 ( 2004-07-03)
那个什么qingchunjingwu,没看清楚人家程序就乱发表评论,人家写的是
this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount -1;
你写成this.DataGrid1.CurrentPageIndex--,这是一回事吗?!



CSDN 网友 ( 2004-07-02)
有的时候真看不下去了,什么垃圾程序都往网上放
qingchunjingwu ( 2004-07-02)
   if (this.DataGrid1.CurrentPageIndex == 0)
    {
     this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount -1;
    }
这句更逗,你写成this.DataGrid1.CurrentPageIndex--;
一句完事了。写得好象初级程序员一样
qingchunjingwu ( 2004-07-02)
 int bdID = int.Parse(DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString());
这句写得真是多余啊,
DataGrid1.DataKeys[e.Item.ItemIndex]本身就是int,你转来转去的累不累?