Delete DataSet China Bank Data Tips

zhaozj2021-02-16  109

Problems when deleting DataSet data: ----------------------------------------- ----

Using

System;

Using

System.collections;

Using

System.componentmodel;

Using

System.data;

Using

System.drawing;

Using

System.Web;

Using

System.Web.SessionState;

Using

System.Web.ui;

Using

System.Web.ui.WebControls;

Using

System.Web.ui.htmlControls;

Using

System.data.sqlclient;

Namespace

TestDeldataSet

{/ ** //// ????? ///? WebForm1? Abstract. ???? /// public class WebForm1: System.Web.UI.Page {protected System.Web.UI.WebControls.DataGrid DataGrid1; protected SqlDataAdapter adapter; protected SqlConnection myConnection; protected DataSet ds???????? PROTECTED? DATATABLE? DT; protected? DATAROW? DR; private? Void? Page_load (Object? Sender,? System.eventargs? E) {? Place user code here to initialize page if (! Page.ispostback) {binddata ();}} private void BindData () {myConnection = new SqlConnection ( "server = localhost; database = Northwind; Trusted_Connection = yes; user id = sa; password =;"); adapter = new SqlDataAdapter ( "select * from Categories ", myconnection; ds = new dataset (); Adapter.Fill (DS," categories "); DT = DS.TABLES [" categories "]; this.dataGrid1.datasource = DT.default1.DATABIND (THISDATAGRID1.DATABIND) ViewState ["DT"] = dt; viewState ["pk"] = dt.primarykey;} Web? Form Designer generated code #REGION? Web? Form Designer Generated Code Override Protected Void? Oninit Eventargs e) {// ???????????? //? Codegen :? This call is necessary for the ASP.NET? Web? Form Designer. ?????????????? initializecomponent (); base.onit (e);} ** //// ????????? ///? Designer support required Method? -? Don't use the code editor to modify ???????? ///? This method is.

???????? /// private void InitializeComponent () {this.DataGrid1.DeleteCommand = new System.Web.UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_Delete);?? This.DataGrid1.SelectedIndexChanged = new ? System.EventHandler (this.DataGrid1_SelectedIndexChanged); this.Load = new System.EventHandler (this.Page_Load);???} #endregion public void DataGrid1_SelectedIndexChanged (??? object sender, System.EventArgs e) {} public void DataGrid1_Delete (object sender, DataGridCommandEventArgs e) {try {DataTable dt = (DataTable) ViewState [ "dt"];? dt.PrimaryKey = new DataColumn [] {dt.Columns [ "CategoryID"]}; int key = (int) THISDATAGRID1.DATAKEYS [(int) E.Item.itemindex]; Response.write ("Click" key.toString () "? color"); // DR = dt.rows.find (key); if (DR.Equals (")) Response.write (" null "); Else Response.write (" no? null "); Dt.Rows.Remove (DR); Dr.Delete (); Dr.acceptchanges () DataGrid1.datasource = DT.DEFAULTVIEW; DATAGRID1.DATABIND ();} catch (system.nullreferenceException pp) {response.write (pp.Message);} catch (SYST Em.argumentException ae) {response.write (ae.message);}}}} ------------------------------- ------------- Due to deleting a row, there is no saving status, so when you delete a row or back, the original deleted line will appear, solve this problem plan is In the initial time, put the DataTable in the DataSet in ViewState, you can save the role of the status, and then remove the DataTable from the ViewState in the delete event, and then call Remove () to delete.

转载请注明原文地址:https://www.9cbs.com/read-11032.html

New Post(0)