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.DataBind () } 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 ();}}}