SlideShare a Scribd company logo
2
Most read
3
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

PPTX
Mysql Aggregate
PPT
Smarter Interfaces with jQuery (and Drupal)
ODP
Mechanize at the Ruby Drink-up of Sophia, November 2011
PPTX
Java script events
DOC
Java script frame history
 
DOCX
How to catch query string in angular js
PPTX
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
PDF
Violet Peña - Storybook: A React Tool For Your Whole Team
Mysql Aggregate
Smarter Interfaces with jQuery (and Drupal)
Mechanize at the Ruby Drink-up of Sophia, November 2011
Java script events
Java script frame history
 
How to catch query string in angular js
Building a Blogging System -- Rapidly using Alpha Five v10 with Codeless AJAX...
Violet Peña - Storybook: A React Tool For Your Whole Team

What's hot (20)

PDF
2013-06-25 - HTML5 & JavaScript Security
PDF
Design for succcess with react and storybook.js
PDF
Rails 3 And The Real Secret To High Productivity Presentation
PPTX
Message Box in JS
PDF
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
DOC
Vijesh Pachatiyan 2016
PPT
Perl gui
PPT
e computer notes - Aggregating data using group functions
DOC
Resume.courtneywalker
PDF
Functional testing with capybara
PPTX
Operators
PDF
Telling Stories With RSpec
PPTX
Top Wordpress dashboard hacks
PPTX
PHP Array very Easy Demo
KEY
Jasmine frontinrio
PDF
Broadleaf Presents Thymeleaf
PDF
Embracing Capybara
PPTX
Best practices for js testing
PPT
Changing Template Engine
PPTX
Postgre sql index
2013-06-25 - HTML5 & JavaScript Security
Design for succcess with react and storybook.js
Rails 3 And The Real Secret To High Productivity Presentation
Message Box in JS
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
Vijesh Pachatiyan 2016
Perl gui
e computer notes - Aggregating data using group functions
Resume.courtneywalker
Functional testing with capybara
Operators
Telling Stories With RSpec
Top Wordpress dashboard hacks
PHP Array very Easy Demo
Jasmine frontinrio
Broadleaf Presents Thymeleaf
Embracing Capybara
Best practices for js testing
Changing Template Engine
Postgre sql index
Ad

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

PDF
Step by Step Asp.Net GridView Tutorials
PPTX
Application of Insert and select notes.ppt x
PPT
ASP.NET Session 13 14
PPTX
PPT
Chapter12 (1)
PPTX
Detail view in distributed technologies
DOC
Soa lab 3
PPTX
Grid Vew Control VB
DOCX
Grid view control
DOCX
PPTX
Grid View Control CS
PPTX
Microsoft asp.net online training
PPTX
Microsoft asp.net online training
PPTX
MICROSOFT ASP.NET ONLINE TRAINING
PPTX
MICROSOFT ASP.NET ONLINE TRAINING
PPTX
Microsoft asp.net online training
PPTX
MICROSOFT ASP.NET ONLINE TRAINING
PPTX
MICROSOFT ASP.NET ONLINE TRAINING
Step by Step Asp.Net GridView Tutorials
Application of Insert and select notes.ppt x
ASP.NET Session 13 14
Chapter12 (1)
Detail view in distributed technologies
Soa lab 3
Grid Vew Control VB
Grid view control
Grid View Control CS
Microsoft asp.net online training
Microsoft asp.net online training
MICROSOFT ASP.NET ONLINE TRAINING
MICROSOFT ASP.NET ONLINE TRAINING
Microsoft asp.net online training
MICROSOFT ASP.NET ONLINE TRAINING
MICROSOFT ASP.NET ONLINE TRAINING
Ad

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Approach and Philosophy of On baking technology
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
A Presentation on Artificial Intelligence
PPTX
Cloud computing and distributed systems.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Unlocking AI with Model Context Protocol (MCP)
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation_ Review paper, used for researhc scholars
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Approach and Philosophy of On baking technology
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
A Presentation on Artificial Intelligence
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing

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