When using the DataGrid paging, in normal case, the binding database list record will automatically generate a paging effect, but I will find "invalid CurrentPageIndex values when I delete records. It must be greater than or equal to 0 and less than PageCount. "The exception is actually solving this problem. We have to do it to determine the value of CurrentPageIndex in the DataGrid1_DeleteCommand event, and bind DataGrid according to different results.
// Retrieve the function of the database public Dataset getzcbd () {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;}}
// Bind DataGrid Private Void BindGrid () {DataSet DS = New Dataset (); DS = US.Getzcbd (); if (DS! = Null) {this.dataGrid1.datasource = DS; this.DataGrid1.Database } Else {msg.alert ("load data error!", Page);}} // delete database record function public string deletezcbd (int bdid) {
INT count = this.ifexisezysx (bdid); // does not have to ignore the second sentence, the default count = 1 if (count <= 0) Return "false"; else {string sqlstr = "delete from zcwhere id =" bdid; OLEDBCOMMAND CMD = New OLEDBCOMMAND (SQLSTR, CONN);
Cn.open ();
Try {cmd.executenonQuery (); return "true";} catch (exception e) {return E.MESSAGE.TOSTRING ();} finally {conn.close ();}}}
// DataGrid1_DeleteCommand event modification function 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 ("Delete Data Error!", Page);}} Note: MSG is A MessageBox dialog that is similar to WinForm doesn't matter. You can use Label.Text instead of the code very mess, please understand!
Thank you, my friend Xiaolin has provided technical support here, he is an excellent software engineer.