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

當前位置:雨林木風下載站 > 技術開發(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)下載排行

主站蜘蛛池模板: 中国又粗又大又爽的毛片 | 成年男女男精品免费视频网站 | www.草草草| 欧美自拍在线 | 国产成人精品日本亚洲语言 | 色九九视频 | 日日干狠狠操 | www.久久av.com | 五月天婷婷免费观看视频在线 | 免费在线看a | 亚洲国产成人久久午夜 | 斗罗大陆免费完整观看樱花动漫 | 99久久国产综合精品国 | 玖玖玖免费观看视频 | 91香蕉国产亚洲一二三区 | 国产亚洲欧美精品久久久 | 日本中文字幕在线精品 | 午夜欧美| 国产成人精品免费视频软件 | 99久久精品免费观看国产 | 国产福利永久在线视频无毒不卡 | 欧美中文字幕在线视频 | 东京久久| 国产在线综合一区二区三区 | 波多野结衣 久久 | 91短视频黄 | 在线观看中文字幕 | 久久精品亚洲 | 日本a级片免费 | 波多野结衣一区二区三区在线观看 | 婷婷亚洲综合 | 亚洲婷婷第一狠人综合精品 | 亚洲va高清中文字幕 | 国产精品一二三区 | 亚洲欧美成人综合 | 国产区亚洲区 | 亚洲日韩在线视频 | 水蜜桃爱爱yy视频在线观看 | 亚洲伊人色综合影院 | 国产欧美日韩不卡在线播放在线 | 亚洲精品久久99久久 |