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

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

主站蜘蛛池模板: 欧美在线视频一区二区三区 | 一本色道久久88亚洲综合 | 91麻豆精品一二三区在线 | 嫩草影院一二三永久在线观看 | 国产高清天干天天视频 | 亚洲成人h | 91青青青国产在观免费影视 | 香蕉eeww99国产在线观看 | 18av18av在线播放 | 国产精品卡哇伊小可爱在线观看 | 最新69国产成人精品免费视频动漫 | 免费一级欧美片在线观免看 | 天天干天天干天天干天天 | 96av在线| 黄网址在线看 | 久久久精品久久视频只有精品 | 天天干天天噜 | 欧美不在线 | 国产福利免费在线观看 | 91视频看| 国产亚洲欧美一区二区 | 97久久草草超级碰碰碰 | 杨幂精品国产专区91在线 | 91专区在线观看 | 国产精品综合视频 | 日韩欧美中文字幕在线观看 | 日本一级毛片在线播放 | 日韩一区二区三区在线 | 日本中文字幕视频 | 黄黄的网站在线观看 | 最近免费的中文字幕一 | 水蜜桃在线视频 | 香蕉视频a | 亚洲一区二区三区在线播放 | 中文字幕在线观看亚洲日韩 | 亚洲精品123区 | 91在线看视频 | 国产亚洲视频网站 | 日本久久精品 | 亚洲 中文 欧美 日韩 在线 | 在线国产资源 |