Editing Data in the DataGrid


We looked at how to use the DataGrid to display data. The next step is to allow the user to update the data. The DataGrid has built-in events that make the updating task relatively simple. The DataGrid has a property named EditItemIndex that is usually set to -1, indicating that no row is currently being edited. We use this property to control the appearance of the DataGrid and work with the three events related to editing data: EditCommand, CancelCommand, and UpdateCommand.

Typically, you enable editing by setting the EditItemIndex in the EditCommand event handler, and abandon the changes by setting the EditItemIndex back to -1 in the CancelCommand event handler. The update is performed when the user submits changes within the UpdateCommand event handler.

The first step is to provide an EditCommandColumn and provide text for the LinkButtons control that will be automatically rendered. When EditItemIndex is -1, the value of the EditText attribute is displayed in the EditCommandColumn; otherwise, the values of the UpdateText and CancelText attributes are displayed for the selected row. When the user clicks a LinkButton, the EditCommand event handler for the control is invoked. In Code Listing 3-13, notice that the DataGrid is bound at the end of the event handlers so that the updated view is rendered for the user.

Code Listing 3-13: DataGridEdit.aspx

start example
 <%@Import namespace="System.Data" %>
<script language="C#" runat="server">
DataTable data;
protected void Page_Load(object o, EventArgs e) {
GetData();
datagrid1.DataSource = data;
if(!IsPostBack) {
datagrid1.DataBind();
}
}

DataTable GetData() {
data = Session["data"] as DataTable;
if(data != null) {
return data;
}
data = new DataTable();

DataColumn primaryColumn
= new DataColumn("carid", typeof(Int32));
data.Columns.Add(primaryColumn);
data.Columns.Add(new DataColumn("year", typeof(Int32)));
data.Columns.Add(new DataColumn("make", typeof(string)));
data.Columns.Add(new DataColumn("model", typeof(string)));
DataRow dr;
dr = data.NewRow();
dr[0] = 1; dr[1] = 1998; dr[2] = "Isuzu"; dr[3] = "Trooper";
data.Rows.Add(dr);
dr = data.NewRow();
dr[0] = 2; dr[1] = 2000; dr[2] = "Honda"; dr[3] = "Civic";
data.Rows.Add(dr);

DataColumn[] primaryColumns = new DataColumn[1];
primaryColumns[0] = primaryColumn;
data.PrimaryKey = primaryColumns;

Session["data"] = data;
return data;
}

protected void OnEdit(object o, DataGridCommandEventArgs e) {
datagrid1.EditItemIndex = e.Item.ItemIndex;
datagrid1.DataBind();
}

protected void OnCancel(object o, DataGridCommandEventArgs e) {
datagrid1.EditItemIndex = -1;
datagrid1.DataBind();
}

protected void OnUpdate(object o, DataGridCommandEventArgs e) {

int year;
try {
year = Int32.Parse((
(TextBox)e.Item.Cells[2].Controls[0]).Text);
}
catch(System.FormatException fe) {
Response.Write(
"Invalid year specified. Update not completed");
return;
}
string make = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string model = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
DataRow row = data.Rows.Find(e.Item.Cells[1].Text);
if(row != null) {
row["year"] = year.ToString();
row["make"] = make;
row["model"] = model;

}
data.AcceptChanges();
datagrid1.EditItemIndex = -1;
datagrid1.DataBind();
Session["data"] = data;
}
</script>

<form runat="server">
<asp:DataGrid runat="server" DataKeyField="carid"
AutoGenerateColumns="false" OnEditCommand="OnEdit"
OnCancelCommand="OnCancel" OnUpdateCommand="OnUpdate" >
<Columns>
<asp:EditCommandColumn EditText="Edit"
CancelText="Cancel" UpdateText="Update" />
<asp:boundcolumn DataField="carid" ReadOnly="true"
HeaderText="id" />
<asp:BoundColumn DataField="year" HeaderText="year" />
<asp:BoundColumn DataField="make" HeaderText="make" />
<asp:BoundColumn DataField="model" HeaderText="model" />
</Columns>
</asp:DataGrid><br/>
</form>
end example

The CancelCommand event handler simply sets the EditItemIndex back to -1, implicitly abandoning any modified data, and calls DataBind.

The majority of the work is usually done in the UpdateCommand event handler. In Code Listing 3-13, in which we bind to data in the page and temporarily store the data in Session state, we find the row in the DataTable and update it with the text from the updated DataGridItem. Notice that we index into the TableRow contained by the DataGridItem to retrieve the TextBox that was used to allow the user to update the item. Don’t forget to update the data source—in this example, that means updating the Session data. In many situations, the DataGrid is bound against data that is disconnected from the back- end database and manipulated locally, as shown in this example. Although the data might continue to look correct to the user, the developer must explicitly commit any local changes back to the real data source.




Microsoft ASP. NET Coding Strategies with the Microsoft ASP. NET Team
Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
ISBN: 073561900X
EAN: 2147483647
Year: 2005
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net