Hello..,
Given Below
is the most commonly used method:
Suppose we want to make the FirstName field of Gridview editable.
Step 1: Add 'Edit' button
column to the Gridview. For that go to Gridview Tasks-->Add new Column -->Select 'Choose a field Type' as 'CommandField' &
'Command
Button' as 'Edit/Update'. Now a column with 'edit' is added to your Gridview.
Step 2:Go to
Gridview Smart Tag--> Edit Templates-->choose EditItemTemplate for the column we want to make editable. Add a TextBox say 'txtFirstName' from the toolbox
to the EditItemTemplate field. Goto'txtFirstName' -->Edit DataBindings--->Text -->In Custom Binding, give Bind("FirstName") // give the corresponding
table column name here //
(If a TemplateField lacks an EditItemTemplate the GridView column will be non-editable.)
Step 3:Use the Gridview events, RowEditing() & RowUpdating()
protected void
GridView1_RowEditing(object sender, GridViewEditEventArgs e) // invoked when the edit button is clicked.
{
GridView1.EditIndex = e.NewEditIndex; // taking the index of the row being edited.
fn_grid_fill(); // Call the function to fill the Gridview
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string firstname = ((TextBox)GridView1.Rows[e.RowIndex].Cells
["FirstName"].FindControl("txtFirstName"));
fn_update_database(firstname);
}