Master ASP.NET Core 2.1 : Instructor-led online course conducted by Bipin Joshi. Next batch starting from 30 December 2018. Limited seats. More details are available 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 software consultant, trainer, author, yoga mentor, and spiritual guide having 23+ years of experience in software development, consulting, and training. He conducts instructor-led online training courses in ASP.NET Core, ASP.NET MVC, and Design Patterns for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 25 August 2002


Tags : ASP.NET Web Forms Server Controls Data Controls


Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.