Click here to Skip to main content
Licence CPOL
First Posted 12 Feb 2009
Views 29,131
Downloads 720
Bookmarked 19 times

Edit Cells in Gridview in ASP.NET

By | 12 Feb 2009 | Article
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:
    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)

About the Author

Primejdie



Romania Romania

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembersugumar srinuvas19:30 6 Nov '11  
GeneralMy vote of 5 Pinmembergrivix15:41 3 Apr '11  
GeneralMy vote of 1 PinmemberPetr Sikula0:41 9 Feb '10  
GeneralMy vote of 1 PinmemberDaniel Nutu22:31 1 Feb '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 12 Feb 2009
Article Copyright 2009 by Primejdie
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid