Introduction:
In this article,we will discuss about how to allow users to edit web server control.
Main:
1.Select datagrid,select Design view, select the DataGrid control, then click the Property Builder
link at the bottom of the Properties window.
In the DataGrid Properties dialog box, click the General tab. If you want to associate
data from a key field with each row in the grid, select a field name from the Data key field list.
You can use this information to update a specific item in the data source.(Normally its might be any Primay key Column)
2.Add Edit Command,Update Command,Cancel command in itemtemplate.
3.Add Edit Command event in Code Behind,
private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
4.Add Update Command Event in Code Behind,
private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// The quantity is in the 7th column.
TableCell quantityCell = e.Item.Cells[6];
// The TextBox is the 0th element of the Controls collection.
TextBox quantityBox = (TextBox)quantityCell.Controls[0];
// Extract the quantity from the box.
int quantity = System.Int32.Parse(quantityBox.Text);
// Use quantity to update the data source.
// Switch out of edit mode.
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
5.Add cancel Command event
private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
Conclusion:
Allowing users to update data table is needed for Enterprise app’s
Hope,this helps
Happy Coding.
References:
http://msdn.microsoft.com