在使用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)