Click here to Skip to main content
15,891,473 members
Articles / Web Development / ASP.NET
Article

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 174.2K   3.3K   77   29
Yet another insertable ASP.NET GridView control.

Insertable gridview

Introduction

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:

VB
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.

History

  • 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.

License

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


Written By
Web Developer Forthnet
Greece Greece
Software developer and Microsoft Trainer, Athens, Greece (MCT, MCSD.net, MCSE 2003, MCDBA 2000,MCTS, MCITP, MCIPD).

Comments and Discussions

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

1. The design view shows broken links for the images even if they exist at newly designated paths in the various image path properties. They only show up in design view when they are at the default image path. They work fine in run-mode though. BTW, have you considered using an embedded resource for those images and just override them if the path properties are set? I recall seeing a good article here on CodeProject about how to do embedded resources in a web control not too long ago.

2. The run-mode does not have the appropriate background color for the header. It looks fine in the design view though. I think the background color property just isn't getting carried through to run-mode because it looks like it's using the same color as your screenshots.

3. The grid seems to make an assumption that the new row that was just added will be at the bottom of the list. In my case, I was using a table with a clustered index on fields other than the primary key and the new row using default values was in the middle of the list. I haven't dug into things yet to see how tricky it would be to handle this scenario. My workaround was to change my default value set in the Inserting event for one of the clustered index fields to ensure the new row would be at the bottom of the grid.

Nice job on the control. I am just now starting to dig into making my own extended web controls and I may use your source as a starting point for a personal project I'm working on. One thing I want to investigate is whether a new row can be handled outside of the data source and only saved to the database once the user commits the changes. That could go a long way to handling item 3 above Smile | :) I'm also going to experiment with using an embedded resource for those images.

--Keith

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.