Click here to Skip to main content
15,395,859 members
Articles / Web Development / ASP.NET
Posted 23 Feb 2006


77 bookmarked

An extended GridView that allows inserting rows

Rate me:
Please Sign up or sign in to vote.
4.51/5 (14 votes)
7 Mar 2006Ms-RL3 min read
Yet another insertable ASP.NET GridView control.

Insertable gridview


This article describes (yet another) insertable GridView. You probably know that the GridView control is the preferred control in ASP.NET v.2 to display and edit tabular data. Although GridView is improved over the previous DataGrid control, it still lacks the capability of inserting new rows. Microsoft says that you can use the FormView or DetailsView components to insert a new row. However, often it is convenient to add a new row in-place. The modified web server GridView control I present makes this possible by adding a 'plus' image in the footer row. When pressed, a new row is added to the database with default values, and the grid shows the new row in editing mode, at the bottom of the last grid page (see image below). If you press the Cancel button, the new row is deleted from the database. The sorting of the GridView is restored after editing the new row.

Insertable gridview when a new row is added

Besides the insert functionality, the code automatically adds button images in the first column of the grid for Select, Edit, and Delete operations.

Using the code

The modified control is implemented as a custom server control that inherits the GridView control. To use it, add a reference to the PSControls.dll file or the PSControls project. If you use the project, you can further customize the control, and also localize the control messages by creating a Messages.XX.resx where XX is your language.

Then, drop a PSGridView control on your .aspx form and configure it using the smart tag menu. The first field (column) must be an empty TemplateField because the code creates the Add, Select, Edit, Delete, Update, and Cancel buttons there. An example of configuring the fields using Visual Studio 2005 is shown below:

Insertable gridview columns

You can determine whether the Add, Select, Edit, and Delete buttons are created, by setting the boolean CanAdd, CanSelect, CanEdit, and CanDelete custom properties of the control, respectively. Additionally, you can disable all editing buttons by setting the IsEditable property to False. By default, all these properties are set to True.

The control adds a new row to the database when the Add button is pressed. So, if the table where you are inserting has 'not null' column(s), an exception will be thrown, unless you put default values for these columns in the Inserting event of the data source control. An example is shown below:

Protected Sub sqlCustomers_Inserting(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) _
      Handles sqlCustomers.Inserting
    e.Command.Parameters("@CustomerType").Value = "S"
End Sub

To have the images appear as in the examples, copy the images folder of the demo at the root of your Web application. Alternatively, specify your own images by means of the AddImageUrl, EditImageUrl, DelImageUrl, SelImageUrl, UpdateImageUrl, and CancelImageUrl properties.

Update: I realized after the initial publication that the insert functionality works correctly only if the primary key of the new row is always incremented. This can be easily implemented by defining an identity (auto-increment) primary key column, e.g., ID int NOT NULL IDENTITY(1,1) PRIMARY KEY, .... Additionally, you must create an empty template for the plus icon to appear when the table has no rows. You can do it with Visual Studio, or by inserting a <EmptyDataTemplate></EmptyDataTemplate> tag into the controls declaration.


  • Version 1.0 - Basic insertable row functionality. Editing controls.
  • Version 1.01 - Updates so that changes of the CanXXX properties on the PreRender event handler takes effect on the control.


This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


About the Author

Philipos Sakellaropoulos
Web Developer Forthnet
Greece Greece
Software developer and Microsoft Trainer, Athens, Greece (MCT,, MCSE 2003, MCDBA 2000,MCTS, MCITP, MCIPD).

Comments and Discussions

BugPSGridview hangs Visual Studio in design mode Pin
philippegent12-Dec-12 3:51
Memberphilippegent12-Dec-12 3:51 
GeneralRe: PSGridview hangs Visual Studio in design mode Pin
Eduardo Sobrinho19-Dec-12 2:21
MemberEduardo Sobrinho19-Dec-12 2:21 
Generalmulti-line editing similar to Excel Pin
ramakant_13526-Mar-09 18:19
Memberramakant_13526-Mar-09 18:19 
QuestionCan a new row contain a dropdownlist? Pin
Member 422925226-Jun-08 8:44
MemberMember 422925226-Jun-08 8:44 
QuestionERROR in INSERT--&gt; CANCEL in new row Pin
zio_pino19-Feb-08 11:54
Memberzio_pino19-Feb-08 11:54 
GeneralInserting and Update Position Pin
Denhoff25-Oct-07 10:14
MemberDenhoff25-Oct-07 10:14 
GeneralSpaces padding insert Pin
bhrenton25-Oct-07 4:42
Memberbhrenton25-Oct-07 4:42 
QuestionIs a Delete Confirmation alert possible? Pin
hgdryhrcyhtchthtchyhtch23-Jun-07 12:40
Memberhgdryhrcyhtchthtchyhtch23-Jun-07 12:40 
AnswerRe: Is a Delete Confirmation alert possible? Pin
IreneT7-Aug-07 11:36
MemberIreneT7-Aug-07 11:36 
GeneralPlease help me Pin
kcoonjah16-Mar-07 1:20
Memberkcoonjah16-Mar-07 1:20 
GeneralCode does not match binaries Pin
GrimaceOfDespair12-Mar-07 10:28
MemberGrimaceOfDespair12-Mar-07 10:28 
QuestionHelp Pin
anupshah291-Mar-07 19:34
Memberanupshah291-Mar-07 19:34 
QuestionCallback Error Pin
CharlieVoss9-Feb-07 7:07
MemberCharlieVoss9-Feb-07 7:07 
AnswerRe: Callback Error Pin
CharlieVoss9-Feb-07 7:26
MemberCharlieVoss9-Feb-07 7:26 
AnswerRe: Callback Error Pin
javiermorales6817-Mar-07 13:08
Memberjaviermorales6817-Mar-07 13:08 
QuestionInsert - C#? Pin
notabenem10-Dec-06 10:49
Membernotabenem10-Dec-06 10:49 
AnswerRe: Insert - C#? Pin
notabenem11-Dec-06 2:36
Membernotabenem11-Dec-06 2:36 
Questionnothing saves Pin
5150.Net19-Jun-06 6:24
Member5150.Net19-Jun-06 6:24 
QuestionNice, but no color changing? Pin
jbakert15-May-06 10:42
Memberjbakert15-May-06 10:42 
AnswerRe: Nice, but no color changing? Pin
Denhoff21-Sep-06 11:06
MemberDenhoff21-Sep-06 11:06 
AnswerRe: Nice, but no color changing? Pin
mbastianon9-Jul-07 3:04
Membermbastianon9-Jul-07 3:04 
GeneralIssues &amp;amp; Questions Pin
olshue14-Mar-06 1:35
Memberolshue14-Mar-06 1:35 
GeneralRe: Issues &amp;amp; Questions Pin
edge942114-Mar-06 14:38
Memberedge942114-Mar-06 14:38 
GeneralRe: Issues &amp;amp; Questions Pin
EspressoShot17-Mar-06 0:59
MemberEspressoShot17-Mar-06 0:59 
GeneralSome minor bugs Pin
edge942111-Mar-06 10:39
Memberedge942111-Mar-06 10:39 

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.