how to edit a template field in a gridview?
how to edit a template field  in a gridview?

See All Responses Below...
Author
Message Rating
Posted on: 03/06/2012
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);
     }


Please Login to Post Your Comments
Name (Max. 100 characters)
Please post your comments here
Select Language
Comments
Attach File(Max. Size: 2 MB)
A few simple rules when posting your Comments,
  1. Please post only answers relevant to the topic of discussion.
  2. Please dont misuse this site or do not be abusive, offensive, inappropriate,harass anyone on the boards or post ads or spam. Doing so will delete your inappropriate messages and will block or delete your account on this site. 

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android