Feel the convenience of DataGrid to data operation (4)

xiaoxiao2021-03-06  75

Section IV: Edit data

I don't know how to modify the database record in the ASP program. Anyway, my approach is to make a list, list all records, then add a hyperlink, then add a hyperlink, then after the user clicks the hyperlink, Forward the page to a new editing page. At that time, I think about editing the Excel form, modify the field I want to modify in my own list. Then, DataGrid provides me with such functions. In DataGrid, you can add a "editing" operation column through the attribute generator in front of the data list, that is, the front (each line) of each record will add a "editing" hyperlink, click this hyperlink, this One way will become editable, each field will become a text box, at this time, the operation column will become "update" and "cancel". This is, we only need to modify the data we need to modify, then click Update Save Programs. (Update Events must write code).

The main steps are as follows:

First, start the attribute generator (do not know how to start, look at the article in front), then select the "column" on the left, in the "available column" on the right, we expand "button column", then, choose "Edit, Update, Cancel", click the ">" button to add it to "Selected Columns" so that our DataGrid will automatically add a column.

Then, return to the attribute window, add three events, one is the event editCommand generated after clicking the "Edit" link, one is an event of updating the data, UpdateCommand, one is a cancel event, canCaleCommand.

Add code in EditCommand, tell the procedure, and choose this line to be edited:

Private void DataGrid1_EditCommand (Object Source, System.Web.ui.WebControls.DataGridCommandEventArgs E)

{

DataGrid1.editItemIndex = (int) E.Item.itemindex; // tells DataGrid Sequence to edit

Bindgrid (); // I wrote in the first section, here is not repeated

}

After completing this code, you will find that it will focus on your choice to edit it according to your instruction. However, when you click "Update", "Cancel" link, it ignores you. This is because we have not written code for these two events, let's write "cancel" code:

Private void DataGrid1_cancelcommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)

{

DataGrid1.editItemIndex = -1; // Transfer -1 to the de-read statid of the DataGrid, it will return to read-only state

Bindgrid ();

}

Simple things, finished, we are doing the most responsible things - update data:

// Due to the case of the case, it has been re-made here.

Private void DataGrid1_UpdateCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)

{

String updatecmd = "Update authors set au_id = @ID, au_lname = @lname, au_fname = @fname, phone = @Phone," "address = @address, city = @city, state = @State, Zip = @zip, contract = @Contract where au_id = @Id "; // should be aware of parametric programming ADO.net in it SqlCommand myCommand = new SqlCommand (updateCmd, myConnection); myCommand.Parameters.Add (new SqlParameter (" @ Id ", SqlDbType .Nvarchar, 11)); MyCommand.Parameters.add (New Sqlparameter ("@ lname", sqldbtype.nvarchar, 40)); mycommand.parameters.add (New Sqlparameter ("@ fname", sqldbtype.nvarchar, 20)) Mycommand.Parameters.add ("@ phone", sqldbtype.nchar, 12)); mycommand.parameters.add (New Sqlparameter ("@ address", sqldbtype.nvarchar, 40)); mycommand.parameters.add (New Sqlparameter ("@ city", sqldbtype.nvarchar, 20)); Mycommand.Parameters.add (New Sqlparameter ("@ State", SqldbType.Nchar, 2)); MyCommand.Parameters.Add (New Sqlparameter) Zip ", Sqldbtype.Nchar, 5)); Mycommand.Parameters.Add (New Sqlparameter (" @ contract ", sqldbtype.nvarchar, 1)); MyCommand.Parameters [@ ID "] .Value = mydatagrid.dataKeys [(int) E.Item.itemindex]; // This place forgot to tell you, we must set DataKeyfield properties in DataGrid, mydatagrid.dataKeys [(int) E.Item.ItemIndex] Come to get the keyword you want

INT Numcols = E.Item.cells.count; for (int i = 2; i

MyCommand.Parameters [cols [i-1]]. value = colvalue;}

Mycommand.connection.open (); try {// Update data

MyCommand.executenonQuery (); // Restore read-only state mydatagrid.edititemindex = -1;} catch (sqlexception eXC) {// error handling

} myCommand.connection.close (); bindgrid (); // Re-bound data, use or write the same one

}

Ok, our work is done, this code looks very long, in fact, because it uses more fields, so I have to use more parameters, I use the key part with red characters. In fact, it is still very simple.

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

New Post(0)