Tutorials to .com

Tutorials to .com » Dotnet » Database » DataGrid common solution: a state in the page to delete records

DataGrid common solution: a state in the page to delete records

Print View , by: iSee ,Total views: 10 ,Word Count: 502 ,Date: Mon, 8 Jun 2009 Time: 6:23 PM

DataGrid paging in the use of the time, under normal circumstances, binding list database will record the effect of automatically generated pages, but I found that the deletion of a record in a time when the Federation took place "invalid value CurrentPageIndex. It must be greater than or equal to 0 and less than PageCount. "abnormal, in fact, very simple to solve this problem, we have to do is DataGrid1_DeleteCommand incident in CurrentPageIndex value judgments, and in accordance with the results of different binding DataGrid.

/ / Search database function
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;
)

)

/ / Bind DataGrid
private void BindGrid ()
(
DataSet ds = new DataSet ();
ds = us.GetZcbd ();
if (ds! = null)
(
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind ();
)
else
(
msg.Alert ( "Error loading data!", Page);
)
)

/ / Delete database records function
public string DeleteZcbd (int bdID)
(

int count = this.IfExiseZysx (bdID); / / do not have to bother at sentence, the default 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 events modified 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);
)

)
Notes: msg for a similar winform the messagebox dialog box, do not have to bother. Can be used in place of label.Text





.Net Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.