SlideShare a Scribd company logo
3
Most read
4
Most read
5
Most read
Add row in asp.net Gridview on button click using C# and vb.net
In this article I am going to explain how to add a row in gridview to insert the record into
database on add new button click in asp.net.
In the previous article I have explained how to avoid (prevent) duplicate record insert on
page refresh in asp.net , how to auto generate and display Serial number (row number) in
asp.net gridview and how to check uncheck OR select/deselect checkboxes in asp.net
gridview control using Jquery.
Description:
We have 2 ways to implement this functionality. 1st
one to add new row to Gridview dynamically
(code behind). Toimplementthisrefertothislink how toaddmultiple rowstoGridview control
dynamicallyinasp.net.2nd
isuse the Gridview FooterTemplate.Putall the required control infooter
template andenable the footertemplate onbuttonclick.
Implementation:
HTML Markup:
<asp:Button ID="btnaddrow" runat="server" Text="Add New Record" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" DataKeyNames="Id"
CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Movie Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# Eval("Name")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtname" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Enter Movie Name"
ControlToValidate="txtname"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Genre">
<ItemTemplate>
<%# Eval("Genre") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtgenre" runat="server" Text='<%# Eval("Genre")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtgenre" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Enter Genre"
ControlToValidate="txtgenre"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<%# Eval("Budget") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtbudget" runat="server" Text='<%# Eval("Budget")
%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtbudget" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Enter Budget"
ControlToValidate="txtbudget"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit"
CausesValidation="false"/><asp:Button ID="btndelete" runat="server" Text="Delete"
CommandName="Delete" CausesValidation="false" CssClass="btn"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnupdate" runat="server" Text="Update"
CommandName="Update" CausesValidation="false"/><asp:Button ID="btncancel"
runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"
CssClass="btn" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btninsert" runat="server" Text="Insert Record"
CommandName="Insert" />
</FooterTemplate>
<ItemStyle VerticalAlign="Top" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Add the namespace
C# code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.net code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Bind data to Gridview
Create a function to fetch the record from database and bind to Gridview and call the
function on page laod.
C# code:
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
public void BindGridview()
{
try
{
SqlDataAdapter adp = new SqlDataAdapter("Select * from Tb_Movie", con);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
}
}
VB.net code:
Dim con As New
SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridview()
End If
End Sub
Public Sub BindGridview()
Try
Dim adp As New SqlDataAdapter("Select * from Tb_Movie", con)
Dim dt As New DataTable()
adp.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Catch ex As Exception
End Try
End Sub
Enable the Gridview Footer
On add new record button click write the below given to show the FooterTemplate of
gridview.
C# code:
protected void btnaddrow_Click(object sender, EventArgs e)
{
GridView1.ShowFooter = true;
BindGridview();
}
VB.net code:
Protected Sub btnaddrow_Click(sender As Object, e As System.EventArgs) Handles
btnaddrow.Click
GridView1.ShowFooter = True
BindGridview()
End Sub

More Related Content

DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PDF
3. Java Script
PPTX
Java abstract class & abstract methods
PPTX
Java script
PPT
JavaScript Tutorial
PPTX
Java script errors &amp; exceptions handling
PDF
Let us c(by yashwant kanetkar) chapter 2 solution
PPT
Abstract class
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
3. Java Script
Java abstract class & abstract methods
Java script
JavaScript Tutorial
Java script errors &amp; exceptions handling
Let us c(by yashwant kanetkar) chapter 2 solution
Abstract class

What's hot (20)

PPTX
Standard Template Library
PPT
CSS
DOCX
Practical file on web technology(html)
PPTX
Asp.NET Validation controls
PPT
How Cascading Style Sheets (CSS) Works
PDF
JavaScript - Chapter 1 - Problem Solving
PPT
PPT
Vi editor in linux
PPTX
Java Strings
PPTX
Form Validation in JavaScript
PPT
Java And Multithreading
PPT
Java Threads
PDF
Bootstrap
PPTX
HTML, CSS and Java Scripts Basics
PPT
Java Networking
PDF
Javascript Basic
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
PPT
Exception Handling in JAVA
PDF
Javascript
PPTX
Strings in Java
Standard Template Library
CSS
Practical file on web technology(html)
Asp.NET Validation controls
How Cascading Style Sheets (CSS) Works
JavaScript - Chapter 1 - Problem Solving
Vi editor in linux
Java Strings
Form Validation in JavaScript
Java And Multithreading
Java Threads
Bootstrap
HTML, CSS and Java Scripts Basics
Java Networking
Javascript Basic
Let us C (by yashvant Kanetkar) chapter 3 Solution
Exception Handling in JAVA
Javascript
Strings in Java
Ad

Similar to Add row in asp.net Gridview on button click using C# and vb.net (20)

PPTX
Detail view in distributed technologies
DOCX
Cis 407 i lab 5 of 7
DOC
Ôn tập KTTMDT
PDF
RichFaces: more concepts and features
DOCX
2. A parking garage charges a $2.00 minimum fee to park for up to thr.docx
PPT
SynapseIndia creating asp controls programatically development
PDF
Un-Framework - Delivering Dynamic Experiences with HTML over the Wire
PDF
Documentation For Tab Setup
PPT
Dat402
PPT
Advanced dot net
PDF
The Ring programming language version 1.6 book - Part 47 of 189
DOCX
Cis 407 i lab 4 of 7
PDF
The Ring programming language version 1.2 book - Part 33 of 84
PPTX
Uniface Lectures Webinar - Building Responsive Applications with Uniface: Dev...
PPTX
Implementation of GUI Framework part3
PDF
Vaadin Components @ Angular U
DOCX
Implement a Javascript application that allows the user to enter strin.docx
PDF
ASP.Net, move data to and from a SQL Server Database
DOC
Tutorial asp.net
PPT
Form demoinplaywithmysql
Detail view in distributed technologies
Cis 407 i lab 5 of 7
Ôn tập KTTMDT
RichFaces: more concepts and features
2. A parking garage charges a $2.00 minimum fee to park for up to thr.docx
SynapseIndia creating asp controls programatically development
Un-Framework - Delivering Dynamic Experiences with HTML over the Wire
Documentation For Tab Setup
Dat402
Advanced dot net
The Ring programming language version 1.6 book - Part 47 of 189
Cis 407 i lab 4 of 7
The Ring programming language version 1.2 book - Part 33 of 84
Uniface Lectures Webinar - Building Responsive Applications with Uniface: Dev...
Implementation of GUI Framework part3
Vaadin Components @ Angular U
Implement a Javascript application that allows the user to enter strin.docx
ASP.Net, move data to and from a SQL Server Database
Tutorial asp.net
Form demoinplaywithmysql
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Cloud computing and distributed systems.
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Newfamily of error-correcting codes based on genetic algorithms
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Advanced IT Governance
PDF
Chapter 2 Digital Image Fundamentals.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
MYSQL Presentation for SQL database connectivity
NewMind AI Monthly Chronicles - July 2025
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Electronic commerce courselecture one. Pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
CroxyProxy Instagram Access id login.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
“AI and Expert System Decision Support & Business Intelligence Systems”
Review of recent advances in non-invasive hemoglobin estimation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Cloud computing and distributed systems.
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Newfamily of error-correcting codes based on genetic algorithms
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Advanced IT Governance
Chapter 2 Digital Image Fundamentals.pdf

Add row in asp.net Gridview on button click using C# and vb.net

  • 1. Add row in asp.net Gridview on button click using C# and vb.net In this article I am going to explain how to add a row in gridview to insert the record into database on add new button click in asp.net. In the previous article I have explained how to avoid (prevent) duplicate record insert on page refresh in asp.net , how to auto generate and display Serial number (row number) in asp.net gridview and how to check uncheck OR select/deselect checkboxes in asp.net gridview control using Jquery. Description: We have 2 ways to implement this functionality. 1st one to add new row to Gridview dynamically (code behind). Toimplementthisrefertothislink how toaddmultiple rowstoGridview control dynamicallyinasp.net.2nd isuse the Gridview FooterTemplate.Putall the required control infooter template andenable the footertemplate onbuttonclick. Implementation: HTML Markup: <asp:Button ID="btnaddrow" runat="server" Text="Add New Record" /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="Id" CellPadding="4" ForeColor="#333333" GridLines="None"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:TemplateField HeaderText="Movie Name"> <ItemTemplate> <%# Eval("Name") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtname" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtname" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Movie Name" ControlToValidate="txtname"></asp:RequiredFieldValidator> </FooterTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="Genre">
  • 2. <ItemTemplate> <%# Eval("Genre") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtgenre" runat="server" Text='<%# Eval("Genre") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtgenre" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter Genre" ControlToValidate="txtgenre"></asp:RequiredFieldValidator> </FooterTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="Budget"> <ItemTemplate> <%# Eval("Budget") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtbudget" runat="server" Text='<%# Eval("Budget") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtbudget" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Enter Budget" ControlToValidate="txtbudget"></asp:RequiredFieldValidator> </FooterTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" CausesValidation="false"/><asp:Button ID="btndelete" runat="server" Text="Delete" CommandName="Delete" CausesValidation="false" CssClass="btn"/> </ItemTemplate> <EditItemTemplate> <asp:Button ID="btnupdate" runat="server" Text="Update" CommandName="Update" CausesValidation="false"/><asp:Button ID="btncancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false" CssClass="btn" /> </EditItemTemplate> <FooterTemplate> <asp:Button ID="btninsert" runat="server" Text="Insert Record" CommandName="Insert" />
  • 3. </FooterTemplate> <ItemStyle VerticalAlign="Top" /> </asp:TemplateField> </Columns> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> </asp:GridView> Add the namespace C# code: using System.Data; using System.Data.SqlClient; using System.Configuration; VB.net code: Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Bind data to Gridview Create a function to fetch the record from database and bind to Gridview and call the function on page laod. C# code: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString()); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)
  • 4. { BindGridview(); } } public void BindGridview() { try { SqlDataAdapter adp = new SqlDataAdapter("Select * from Tb_Movie", con); DataTable dt = new DataTable(); adp.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } catch (Exception ex) { } } VB.net code: Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString()) Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not IsPostBack Then BindGridview() End If End Sub Public Sub BindGridview() Try Dim adp As New SqlDataAdapter("Select * from Tb_Movie", con) Dim dt As New DataTable() adp.Fill(dt) GridView1.DataSource = dt GridView1.DataBind() Catch ex As Exception End Try End Sub Enable the Gridview Footer On add new record button click write the below given to show the FooterTemplate of gridview.
  • 5. C# code: protected void btnaddrow_Click(object sender, EventArgs e) { GridView1.ShowFooter = true; BindGridview(); } VB.net code: Protected Sub btnaddrow_Click(sender As Object, e As System.EventArgs) Handles btnaddrow.Click GridView1.ShowFooter = True BindGridview() End Sub