GridView Row

GridView Row

  In this post we will see, how to add multiple rows. The complete source code can be downloded from here

Download: (3.34 kb) 

 As shown in figure. Here we are adding 12 new rows into existing Grid.

GridView code

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                ForeColor="#333333" GridLines="None">
                            <%# Container.DataItemIndex+1 %>
                    <asp:TemplateField HeaderText="First name">
                            <asp:Label ID="Label1" Text='<%#DataBinder.Eval(Container.DataItem,"FirstName") %>'
                                runat="server" Visible='<% # (DataBinder.Eval(Container.DataItem,"FirstName")!="")?true:false%>'></asp:Label>
                            <asp:TextBox ID="TextBox2" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'
                                Visible='<%# IsNullItem((DataBinder.Eval(Container.DataItem,"FirstName"))) ? true : false %>'
                    <asp:TemplateField HeaderText="Last name">
                            <asp:Label ID="Label2" runat="server"
Visible='<% # (DataBinder.Eval(Container.DataItem,"LastName")!="")?true:false%>'
                                Text='<%#DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
                            <asp:TextBox ID="txtLastName" Visible='<%# IsNullItem((DataBinder.Eval(Container.DataItem,"LastName"))) ? true : false %>'
                    <asp:TemplateField HeaderText="Email address">
                            <asp:Label ID="Label3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Email") %>'
                                Visible='<% # (DataBinder.Eval(Container.DataItem,"Email")!="")?true:false%>'></asp:Label>
                            <asp:TextBox ID="txtEmail" Visible='<%# IsNullItem((DataBinder.Eval(Container.DataItem,"Email"))) ? true : false %>'
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />

Based on wheather the Data is null or empty we are changing visibility of Labelboxes and TextBoxes. 

C#.Net Code:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class GridView_Add_Multiple_rows : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)



protected void LinkButton1_Click(object sender, EventArgs e)


DataTable dt = GridDataProvider.GetData();

for(int i=1; i< Convert.ToInt16(TextBox1.Text); i++)


DataRow dr = dt.NewRow();



GridView1.DataSource = dt;



public bool IsNullItem(Object o)


if( o == System.DBNull.Value)

return true;

else return false;


protected void LinkButton2_Click(object sender, EventArgs e)


foreach(GridViewRow gr in GridView1.Rows)


string email = ((TextBox)gr.FindControl("txtEmail")).Text;

// Here you can call your Data base routines to store values







VB.Net code:

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Partial Class GridView_Add_Multiple_rows
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim dt As DataTable = GridDataProvider.GetData()
        For i As Integer = 1 To Convert.ToInt16(TextBox1.Text) - 1
            Dim dr As DataRow = dt.NewRow()
        GridView1.DataSource = dt
    End Sub
    Public Function IsNullItem(ByVal o As [Object]) As Boolean
        If o = System.DBNull.Value Then
            Return True
            Return False
        End If
    End Function
    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As EventArgs)
        For Each gr As GridViewRow In GridView1.Rows
            Dim email As String = DirectCast(gr.FindControl("txtEmail"), TextBox).Text
            ' Here you can call your Data base routines to store values
            If Not String.IsNullOrEmpty(email) Then
            End If
    End Sub
End Class  

IsNullItem function will check wheather data is null or not. It will help us to toggle visibility of Label and textBox.



gridview add new rowGridView Gridview checkboxGridView EditHow to set default browserJquery Gridview Dropdownlist Selected Change Enable Disable get tomorrows dateJquery Table Row Highlight On MouseoverWhat is difference between browser reload and refreshJquery Select Table RowBrowser close event detection


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.