DataGrid use: (1), implement basic operations in the DataGrid control (edit, delete, pagination)

xiaoxiao2021-03-06  89

// make the current line becomes editable DataGrid private void DataGrid1_EditCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {this.DataGrid1.EditItemIndex = e.Item.ItemIndex; this.bindgrid ();}

// used to undo the changes made (trigger click the Cancel button) private void DataGrid1_CancelCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {this.DataGrid1.EditItemIndex = -1; this.bindgrid ();}

// delete the current row use (trigger click the Delete button) private void DataGrid1_DeleteCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {int userid = (int) this.DataGrid1.DataKeys [e.Item.ItemIndex ]; string SQLStatement = "Delete xtuser WHERE user_id =" userid; SqlConnection myConnection = new SqlConnection (strcon); SqlCommand myCommand = new SqlCommand (SQLStatement, myConnection); myCommand.CommandTimeout = 15; myCommand.CommandType = CommandType.Text;

Try {myconnection.open (); myCommand.executenonQuery (); myconnection.close ();} catch (Exception EE) {throw ee;} this.DataGrid1.edititeMindex = -1; // ds.tables ["user"] .Rows.clear (); ds.tables [0] .clear (); this.sqldataadapter1 .fill (DS, "User"); this.bindgrid ();}

// When trigger to click on, for simple paging private void DataGrid1_PageIndexChanged (object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) {this.DataGrid1.CurrentPageIndex = e.NewPageIndex; this.bindgrid ();} // after the modification is completed, click on the button to trigger the update, to achieve the purpose of the modified private void DataGrid1_UpdateCommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) {int userid = (int) this.DataGrid1.DataKeys [e.Item.ItemIndex] String usernc = (textbox) E.Item.cells [1] .controls [0]). Text; string usermc = ((textbox) E.cells [2] .controls [0]). Text; string SQLStatement = "UPDATE xtuser" "SET user_nc = '" usernc "'," "user_mc = '" usermc "'" "WHERE userid =" userid; SqlConnection myConnection = new SqlConnection (this.strcon); Sqlcommand mycommand = new sqlcommand (sqlstatement, myconnection); mycommand.commandtimeout = 15; mycommand.commandtype = commandtype.text;

Try {myconnection.open (); mycommand.executenonury (); myconnection.close ();} catch (exception ee) {throw ee;}

MyDataGrid.edititemindex = -1; this.bindgrid ();

Bind the data to the data void bindgrid () {this.dataGrid1.datasource = DS.TABLES ["user"]. DefaultView; this.DataGrid1.databind ();

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

New Post(0)