To edit a data item in a DataGrid control

Source: Internet
Author: User
Tags exit

To edit data in the DataGrid control, use the edit, update, cancel column in the button column, which can be set in the DataGrid control's Property Builder

When the edit, update, cancel column is added to the DataGrid control (in the case of a control named DG1), a column appears in the DG1 control of the page, and each item in the column is a Linkbutton/button with the text as edit.

If you click the Edit button for a row, the row is in edit mode, the Edit button is replaced with the update and Cancel buttons, and all other non-read-only data in that row becomes a TextBox control format so that the user can edit the changes.

When the user modifies the data that is not read-only (in a TextBox control), clicks the Update button, saves the new value (typically in the database), clicks the Cancel button, and the row exits edit mode.

In order to achieve the effect of converting to row edit mode by clicking the Edit button, you must write the DG1 EditCommand event handling method

In order to achieve the effect of saving the new value by clicking the "Update" button, you must write the DG1 UpdateCommand event handling method

To exit row editing mode to achieve the Click cancel button, you must edit the DG1 CancelCommand event handling method

1) dg1. EditCommand Event Handling Method-Enter the edit mode of the row

private void dg1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
 dg1.EditItemIndex = e.Item.ItemIndex; //设置要编辑的项的索引
 binddg1(); //为dg1绑定数据的方法。设置要编辑的项后,要求重新绑定dg1
}

2) DG1. CancelCommand Event handling Method-Exit edit mode for row

private void dg_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
 dg1.EditItemIndex = e.Item.ItemIndex; //编辑的项的索引为-1,就是不编辑任何项
 binddg1(); //重设EditItemIndex后,要求重新绑定dg1
}

3) DG1. UpdateCommand Event handling Method-Saves the updated value

To save the updated values, first get the new values in the page. In the event-handling approach, there are three main features to implement:

Gets the updated values, updates the values, and exits the row update mode.

Generally, the data displayed in the DataGrid control is obtained from the database tables, so the updated values are saved to the database, and an update SQL statement or stored procedure can be used to perform the updates.

To get the values of the columns in the row in edit mode from the page, you need some tricks. In the Binding column example:

Gets the value of a read-only bound column: e.item.cells[column index]. Text//read-only help set columns in a non-edited state

Gets the value of a read-only bound column: ((TextBox) (e.item.cells[column index). Controls[0]). Text//In edit state

Read-only set columns are typically primary key columns in a table whose column values are used in the WHERE clause of the UPDATE statement and are not generally updated

code example:

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
 //只读绑定列,处于非编辑状态
 string customerid = e.Item.Cells[1].Text;
 //非只读绑定列,处于编辑状态
 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); //执行update语句,进行更新

 DataGrid1.EditItemIndex = -1;   //退出行的编辑模式
 binddg1();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.