Add Rows To GridView Dynamically

When you fill out any form (recruitment etc.) on any government Website or any other site, you have seen a grid in which you can add rows dynamically, shown in the screenshot, given below:
rows

In this blog, we will learn how to add rows to the grid view dynamically. We will take an employee’s example.

Let’s start!!!

  1. Create a simple table with two or three columns. I have created an Employee table Emp.

    table
  2. Now, create a new Website in Visual Studio. Now, drag a grid view on the page.

  3. Now, create SQL connection with the database.

  4. On the page load, we have to take care of the data in the database i.e. if there is data in the database (initially there is no data at present), we have to load the data from the database otherwise, we have to add an empty row in the grid view.

  5. We will create a method, which counts rows from DB, if row count >0, we will load the data from DB, otherwise we will empty the row in the grid view.

  6. We will use the view state to keep the previous data, when we are adding the new rows. I have used view state, because we have to keep the data within the postbacks. View state is nothing, it is just a string, that is stored as a hidden field on the page.

  7. I applied some logic. You can check the source code for more information. The EmpID is not an identity column here. I used some validations in it. You can use the validation, according to your requirements.

Kindly give me your valuable suggestions on it, if there is any mistake or if you like my work.

Ebook Download
View all
Learn
View all