Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET

Edit Cells in Gridview in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.11/5 (14 votes)
12 Feb 2009CPOL2 min read 83K   2.1K   22   5
This article shows how to edit individual cells in gridview

Introduction

This article shows how to edit individual cells in gridview in ASP.NET. This example is in Visual Web Developer 2005 Express Edition.

Using the Code

Download the project, unzip it to somewhere on your hard disk. You'll find a database and a gridview (test this project after you finish reading this article).

For this article, you can use the database from the project. If you use any other database, you have to specify a primary key (this is very important).

Steps to Create the Project Manually  

In your project (I suppose you have a database with a primary key):

  • In the design view, drag a gridview from toolbox (if you can't see it, press ctrl+alt+x).
  • Click the GridView SmartTag to open up its Tasklist.
  • Choose data source (your database). Select all from your table. Then next and finish.

    select.PNG

  • In the tasklist, click 'Auto Format' and select a scheme. 
  • Check 'Enable Paging'. Go to 'Properties' (press F4) and search for 'Page Size'.

    pagesize.PNG

  • In 'Properties', set 'AutoGenerateEditButton' to true.

Edit an Entire Row 

Remember, you can't modify the field which is the primary key. So, we'll edit the fields 'LastName', 'Age' and 'Sport'. For this, we must add the Update statement to our sqldatasource.

  • Click the GridView SmartTag to open up its Tasklist.
  • Click 'Configure Data Source'. at the 'Configure the Select Statement' step, check "Specify a custom SQL statement or stored procedure". Next. Click the 'update' tab and add this code:
    SQL
    UPDATE [Humans] SET LastName = @LastName, Age = @Age, _
    	sport = @sport WHERE [FirstName] = @FirstName

    update.PNG

  • What is @name? These are the parameters that you fill in, when editing the row.
  • The parameter that appears in the Where clause is the primary key of the database. If the Where clause is missing, then all the rows will be updated. 
  • Click Next and Finish. Save(Ctrl+S). On your page, right-click and 'View in browser'.

    edit_row.PNG

Edit Individual Cell

We want to edit only the 'sport' cell and we don't want to see the textboxes for the other cells.

  • Click the GridView SmartTag to open up its Tasklist. 
  • Go to 'configure data source' and modify the update statement so it looks like this:

    update_scor.PNG

  • Click Next and Finish.
  • In the gridview's tasklist, click 'Edit Columns'. Click the 'LastName' field and set 'ReadOnly' to true.

    read_only.PNG

  • Set 'ReadOnly' to true for the 'Age' field.  
  • Click Ok. Save(Ctrl+S). On your page, right-click and 'View in browser'.

    final.PNG

History

  • 12th February, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy vote of 4 Pin
Alireza_136217-Mar-13 21:09
Alireza_136217-Mar-13 21:09 
GeneralMy vote of 5 Pin
sugumaran srinuvasan6-Nov-11 19:30
sugumaran srinuvasan6-Nov-11 19:30 
GeneralMy vote of 5 Pin
grivix3-Apr-11 15:41
grivix3-Apr-11 15:41 
GeneralMy vote of 1 Pin
Petr Sikula9-Feb-10 0:41
Petr Sikula9-Feb-10 0:41 
GeneralMy vote of 1 Pin
Daniel Nutu1-Feb-10 22:31
Daniel Nutu1-Feb-10 22:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.