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

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

主站蜘蛛池模板: 久久久免费视频观看 | 国产精品午夜自在在线精品 | a免费在线观看视频 | 久久久窝窝午夜精品 | sihu国产午夜精品一区二区三区 | 免费的爱爱视频 | 国产粉嫩00福利福利福利 | 久久国产精品一区二区三区 | 欧美成人爽毛片在线视频 | 国产高清久久 | 毛片在线免费 | 欧美啪啪 | 碰碰射 | 国产黑丝 | 女人18毛片特级一级免费视频 | 欧美综合自拍亚洲综合图自拍 | 5g免费影院永久天天影院在线 | 久久伊人中文字幕有码 | 影院成人区精品一区二区婷婷丽春院影视 | 久久久精品2018免费观看 | 亚洲成年网站在线观看 | 狠狠噜天天噜日日噜 | 国产美女主播一级成人毛片 | 3久女bbbwww | 欧美精品在线视频观看 | 欧美亚洲免费久久久 | 3344成年在线视频免费播放男男 | 久操视频在线观看免费 | 日本免费视 | 精品久久九九 | 国产成人永久免费视 | 91麻豆国产精品91久久久 | 久草视频国产 | 三级五月天| 天天躁日日2018躁狠狠躁 | 国产精品久久久久久久久久久搜索 | 久久www成人看片 | 久久国产精品-久久精品 | 521a久久九九久久精品 | 日本在线视频一区二区三区 | 亚洲在线欧美 |