Click here to Skip to main content
Click here to Skip to main content

Edit Cells in Gridview in ASP.NET

, 12 Feb 2009
Rate this:
Please Sign up or sign in to vote.
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
No Biography provided

Comments and Discussions

 
QuestionMy vote of 4 PinmemberAlireza_136217-Mar-13 21:09 
GeneralMy vote of 5 Pinmembersugumar srinuvas6-Nov-11 19:30 
GeneralMy vote of 5 Pinmembergrivix3-Apr-11 15:41 
GeneralMy vote of 1 PinmemberPetr Sikula9-Feb-10 0:41 
GeneralMy vote of 1 PinmemberDaniel Nutu1-Feb-10 22:31 
outdated

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 Feb 2009
Article Copyright 2009 by Primejdie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid