To edit data in the DataGrid control, please use the "Edit, Update, Cancel" column in the "Button Column". These can be set as the DataGrid control in the property generator of the
DataGrid control (take the control name dg1 as an example ), there will be one more column in the dg1 control on the page. Each item in this column is a LinkButton/Button with the text "Edit".
If you click the "Edit" button of a row, the row is in edit mode, the "Edit" button is replaced by the "Update" and "Cancel" buttons, and all other non-read-only data helper columns in the row will be Change it into a TextBox control format so that users can edit and modify it.
When the user modifies the data of the non-read-only data column (in the TextBox control), clicks the "Update" button, saves the new value (usually to the database), clicks the "Cancel" button, the row Exit edit mode.
In order to achieve the effect of switching to row editing mode by clicking the "Edit" button, the EditCommand event handling method of dg1 must be written. In order to achieve the effect of saving the new value by clicking the "Update" button, the UpdateCommand event handling method of dg1 must be written. In order to click the "Cancel" button to exit the line editing mode, you must edit the CancelCommand event handling method of dg1
1) dg1.EditCommand event handling method - enter the line editing mode
-------------------------------------------------- ----
private void dg1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dg1.EditItemIndex = e.Item.ItemIndex; //Set the index of the item to be edited
binddg1(); //Method to bind data to dg1. After setting the item to be edited, it requires rebinding dg1
}
-------------------------------------------------- -------
2) dg1.CancelCommand event handling method - Exit row editing mode
-------------------------------------------------- -------
private void dg_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dg1.EditItemIndex = e.Item.ItemIndex; //The index of the edited item is -1, which means no item is edited.
binddg1(); //After resetting EditItemIndex, it is required to rebind dg1
}
-------------------------------------------------- -------
3) dg1.UpdateCommand event handling method - Save updated values. To save updated values, you must first obtain these new values from the page. In the event processing method, three main functions must be implemented:
Get updated values, update those values, and exit row update mode.
Generally, the data displayed in the DataGrid control is obtained from the database table, so the updated value must also be saved in the database. You can use an update Sql statement or a stored procedure to perform the update.
Getting the values of columns from a page in a row in edit mode requires some tricks. Take binding columns as an example:
Get the value of a read-only bound column: e.Item.Cells[column index].Text //The read-only help column is in a non-editable state Get the value of a non-read-only bound column: ((TextBox)(e.Item. Cells[Column Index].Controls[0])).Text //In editing state,
the read-only help column is usually the primary key column in the table. Its column value should be used in the where clause of the update statement. Generally, they are not updated.
Code example:
-------------------------------------------------- --------
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//Read-only bound column, in non-editable state
string customerid = e.Item.Cells[1].Text;
//Non-read-only bound column, in editing state
string companyname = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string city = ((TextBox)(e.Item.Cells[3].Controls[0])).Text;
String strSql = "update customers set companyname = '" + companyname +
"',city = '" + city + "' where customerid = '" + customerid + "'";
executeSql(strSql); //Execute the update statement to update
DataGrid1.EditItemIndex = -1; //Exit the row editing mode
binddg1();
}