Inserting Multiple Rows Using GridView
ASP.NET GridView control is one of the most popular control when it comes to
displaying and editing tabular data. However, when it comes to inserting data
the GridView has very little to offer. Using a technique as illustrated in my
Inserting a New Row in GridView you can insert a single record using
EmptyDataTemplate of the GridView. However, what if you want to insert multiple
rows using GridView? In real world cases developers often require such a
mechanism. Though there is no out of the box answer to this problem this article
is going to demonstrate a possible solution. Read on...
Have a look at the following figure that shows part of a web form.
The web form consists of a GridView control and two buttons. The interesting
part is that the GridView is showing five rows that are empty. The user can
enter data in those five rows and then click on "Save All" button to save the
data in the database. Clicking on the "CLear Grid" will clear the entered data.
GridView and data source
By design the GridView control is a data bound control. That means it always
requires some data source to bind with. It cannot be used in unbound fashion. In
order to display empty rows in the GridView you need to have a data source that
contains empty items. Remember that there is a difference between "empty data
source" and "data source with empty rows". Once you have such a data source you
can bind it with grid so as to render its rows. Accepting data from the user is
just a matter of creating template columns.
To illustrate how all this works create a new web site using Visual Studio.
Add a new class to the web site named Customers. The following code shows the
public class Customer
private string strCustomerID;
private string strCompanyName;
private string strContactName;
private string strCountry;
public string CustomerID
strCustomerID = value;
public string CompanyName
strCompanyName = value;
public string ContactName
strContactName = value;
public string Country
strCountry = value;
The Customer class consists of four private variables and four public
properties viz. CustomerID, CompanyName, ContactName and Country - that
encapsulate them. We use a generic List of Customer objects to bind with the
GridView. We opted for generic collection over DataSet or DataTable so as to
make our application lightweight.
Now drag and drop a GridView and two buttons as shown in the above figure.
Add four TemplateField columns to the GridView and set their HeaderText property
to Customer ID, Company Name, Contact Name and Country respectively. Design all
the four template columns to have one textbox in the ItemTemplate. (see below).
Open the data bindings editor for the textboxs and bind them with CustomerID,
CompanyName, ContactName and Country columns respectively.
Remember that we are binding Text property of the textboxes with the
properties of the Customer class.
Go in the code behind of the web form and create a private method called
private void BindGrid()
List<Customer> items = new List<Customer>(5);
for (int i = 0; i < 5; i++)
Customer c = new Customer();
GridView1.DataSource = items;
The BindGrid() method creates a generic List of customers with capacity of
five elements. It then runs a for loop to create five objects of Customer class.
The List is then bound with the GridView. Since we just want to insert new
records we need not set any properties of the Customer class. Had it been an
update operation you would have set the properties of Customer objects to the
appropriate values from the database.
The BindGrid() method is called from two places - Page_Load event handler and
Click event handler of "Clear Grid" button.
protected void Page_Load(object sender,
protected void Button2_Click(object sender,
The main job of inserting the entered data goes inside the Click event of
"Save All" button. We also need few of helper methods namely BeginAdd(),
AddCustomer() and CompleteAdd().
The BeginAdd() method looks as shown below:
SqlConnection cnn = new SqlConnection
("data source=.;initial catalog=northwind;
SqlCommand cmd = new SqlCommand();
private void BeginAdd()
SqlTransaction tran= cnn.BeginTransaction();
cmd.Connection = cnn;
cmd.Transaction = tran;
cmd.CommandText = "insert into customers
SqlParameter p1 = new SqlParameter
SqlParameter p2 = new SqlParameter
SqlParameter p3 = new SqlParameter
SqlParameter p4 = new SqlParameter
The code declares a connection and command at the class level. The BeginAdd()
method establishes a connection with the Northwind database and initiates a
transaction. In our example we insert all the records as a single transaction.
You may omit the transactional operation if you don't need it. The code then
configures Connection, Transaction and CommandText properties of the command
object. The CommandText property specifies an INSERT statement that inserts a
record to Customers table of the Northwind database. The code then adds four
parameters to the command object. The BeginAdd() method needs to be called
before actual INSERT operation begins.
The actual INSERT operation is carried out by another method called
private void AddCustomer(string custid, string company,
string contact, string country)
cmd.Parameters.Value = custid;
cmd.Parameters.Value = company;
cmd.Parameters.Value = contact;
cmd.Parameters.Value = country;
The AddCustomer() method accepts four parameters representing customer ID,
company name, contact name and country. It then sets the respective parameter
values of SqlCommand object we configured in BeginAdd() method earlier. Then
ExecuteNonQuery() method of the command object is called. The catch block traps
any exceptions occured during the INSERT operation and calls Rollback() method
of the transaction object. The AddCustomer() method is called multiple times
depending on the rows entered by the user.
The CompleteAdd() method commits the transaction.
private void CompleteAdd()
Label1.Text = "Customers added successfully!";
Label1.Text = "Error completing the operation!";
The ComplateAdd() method calls Commit() method of transaction object and
displays a success or failure message. Finally, it closes the connection that
was opened in BeginAdd() method.
The BeginAdd(), AddCustomer() and CompleteAdd() methods are used in the click
event handler of "Save All" button.
protected void Button1_Click(object sender,
foreach (GridViewRow row in GridView1.Rows)
if (row.RowType == DataControlRowType.DataRow)
string custid = ((TextBox)row.FindControl
string company = ((TextBox)row.FindControl
string contact = ((TextBox)row.FindControl
string country = ((TextBox)row.FindControl
if (custid != "")
AddCustomer(custid, company, contact, country);
The click event handler of the button calls the BeginAdd() method. It then
starts iterating through the rows of the GridView. With each iteration it
extracts the values from the four textboxes using FindControl() method. We
assume that if CustomerID is entered then that record is to be added to the
database. You can add extra validations here. The AddCustomer() method is then
called by passing newly entered customer ID, company name, contact name and
country. Finally, ComplateAdd() method is called to complete the insert
That's it! Our GridView is now ready to accept multiple empty rows and insert
them as a single batch in the database.