日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在分页状态下删除纪录的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在使用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代替

總結

以上是生活随笔為你收集整理的在分页状态下删除纪录的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。