日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當前位置:雨林木風下載站 > 技術開發(fā)教程 > 詳細頁面

DataGrid在分頁狀態(tài)下刪除紀錄的問題

DataGrid在分頁狀態(tài)下刪除紀錄的問題

更新時間:2019-06-21 文章作者:未知 信息來源:網(wǎng)絡 閱讀次數(shù):

在使用DataGrid分頁的時候,正常情況下,綁定數(shù)據(jù)庫列表紀錄時會自動產生分頁的效果,然而我發(fā)覺在刪除紀錄的時候總會發(fā)生"無效的 CurrentPageIndex 值。它必須大于等于 0 且小于 PageCount。"的異常,其實解決這個問題很簡單,我們要做的就是在DataGrid1_DeleteCommand事件中判斷CurrentPageIndex的值,并根據(jù)不同的結果來綁定DataGrid。

//檢索數(shù)據(jù)庫的函數(shù)
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("加載數(shù)據(jù)錯誤!",Page);
}
}

//刪除數(shù)據(jù)庫紀錄函數(shù)
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事件修改函數(shù)
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("刪除數(shù)據(jù)錯誤!",Page);
}

}
注釋:msg為一個類似WinForm的messagebox對話框,不必理會。可以使用label.Text代替

代碼很亂,敬請諒解!

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

主站蜘蛛池模板: 久久超级碰 | 国产91视频观看 | 黄视频网站免费 | 亚洲国产观看 | 不卡午夜视频 | 手机看片福利国产 | 插妞| 欧美精品无播放器在线观看 | 欧美日韩国产一区三区 | 久久久久久久综合 | 国产片18在线观看 | 久久视频这里只有精品35 | 中文有码第一页 | 天天射天天干天天舔 | 欧美在线视频一区在线观看 | 亚洲91精品麻豆国产系列在线 | 久久久久久影视 | 狠狠色综合网 | 亚洲国产一区二区三区四区五区 | 国产欧美中文字幕 | 能直接看的一级欧美毛片 | 日韩综合在线观看 | 久久久国产99久久国产久 | 亚洲国产精品悠悠久久琪琪 | 日本乱人伦免费播放 | 免费91视频 | 美女啪啪网站 | 久久厕所精品国产精品亚洲 | 国产精品大全国产精品 | 国产草草视频 | 一区二区三区在线免费看 | 国产福利视频 | 国产午夜精品久久久久 | 久久美乳 | 久久综合九色综合97小说 | 网曝门精品国产事件在线观看 | 天天综合天天操 | 成人国产在线视频在线观看 | 91看片网| 久久亚洲综合 | 日本猛妇色xxxxx在线 |