Learn ASP.NET Web Forms, ASP.NET MVC, HTML5 and jQuery! Intensive courses for professional developers. Small batches, real world examples, convenient weekend timings. Conducted by Bipin Joshi in Thane. Registration open. Read more details here.

<%@ Page %>

Quickly Adding A New Row In DataGrid

Introduction

ASP.NET DataGrid allow us to provide in-line editing of rows. However, there is no built-in way to add a row. One solution many programmers follow is to add a new row in underlying DataTable and then bind the DataGrid again. This trick does work well but needs some validations on the DataTable before updating. If not tested for all possible user actions the DataTable may contain unwanted empty rows. Alternative to this is to make use of DataGrid footer template to provide empty data entry controls to the user. This is not only a quicker way to add records but also avoids need of strong validations that are otherwise required in former technique.

How It Works?

To provide the user with a blank data entry row, we make use of DataGrid�s footer template. We add TextBoxes directly to the footer template. This makes it unnecessary to click on some button like "Edit". This also reduces number of post backs. We also add a LinkButton (Insert) and set its CommandName property to "Insert". This CommandName is used inside the ItemCommand event handler of DataGrid to ensure that record gets added only when user clicks the Insert LinkButton. The code to add the row in the database is the typical code and needs no explanation.

Sample Application

Sample code is available with this article for downloading that provides you with a fully functional example.

Screen Shot

A sample run of the application looks as follows:


Bipin Joshi is a blogger, author, IT trainer and a Kundalini Yogi who writes about apparently unrelated topics - Yoga & technology! Bipin has been programming since 1995 and is working with .NET framework ever since its inception. He is an internationally published author and has authored or co-authored more than half a dozen books and numerous articles on .NET technologies. He has also penned a few books on Yoga. Bipin was also a Microsoft MVP for six consecutive years. You can read more about him here. To know more about his training programs go here.

Stay updated : Twitter  Facebook  Google+  LinkedIn


Associated Links
Download Source Code

Tags : ASP.NET Web Forms Server Controls Data Controls
Posted On : 25 Aug 2002
Current Rating :
Rate this article :


This page is protected by copyright laws. Copying in any form is strictly prohibited. For Copyright notice and legal terms of use click here.

Protected by Copyscape



Copyright (C) bipinjoshi.net. All rights reserved.
Contact Us
Read Copyright & Terms Of Use
Hosted By DiscountASP.net