SlideShare a Scribd company logo
1 of 5
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

What's hot

Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity PresentationRails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentation
railsconf
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functions
ecomputernotes
 
Operators
OperatorsOperators
Operators
Kumar
 
Best practices for js testing
Best practices for js testingBest practices for js testing
Best practices for js testing
Karl Mendes
 

What's hot (20)

2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
 
Design for succcess with react and storybook.js
Design for succcess with react and storybook.jsDesign for succcess with react and storybook.js
Design for succcess with react and storybook.js
 
Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity PresentationRails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentation
 
Message Box in JS
Message Box in JSMessage Box in JS
Message Box in JS
 
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j queryTraining in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
 
Vijesh Pachatiyan 2016
Vijesh Pachatiyan 2016Vijesh Pachatiyan 2016
Vijesh Pachatiyan 2016
 
Perl gui
Perl guiPerl gui
Perl gui
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functions
 
Resume.courtneywalker
Resume.courtneywalkerResume.courtneywalker
Resume.courtneywalker
 
Functional testing with capybara
Functional testing with capybaraFunctional testing with capybara
Functional testing with capybara
 
Operators
OperatorsOperators
Operators
 
Telling Stories With RSpec
Telling Stories With RSpecTelling Stories With RSpec
Telling Stories With RSpec
 
Top Wordpress dashboard hacks
Top Wordpress dashboard hacks Top Wordpress dashboard hacks
Top Wordpress dashboard hacks
 
PHP Array very Easy Demo
PHP Array very Easy DemoPHP Array very Easy Demo
PHP Array very Easy Demo
 
Jasmine frontinrio
Jasmine frontinrioJasmine frontinrio
Jasmine frontinrio
 
Broadleaf Presents Thymeleaf
Broadleaf Presents ThymeleafBroadleaf Presents Thymeleaf
Broadleaf Presents Thymeleaf
 
Embracing Capybara
Embracing CapybaraEmbracing Capybara
Embracing Capybara
 
Best practices for js testing
Best practices for js testingBest practices for js testing
Best practices for js testing
 
Changing Template Engine
Changing Template EngineChanging Template Engine
Changing Template Engine
 
Postgre sql index
Postgre sql indexPostgre sql index
Postgre sql index
 

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

Cis 407 i lab 5 of 7
Cis 407 i lab 5 of 7Cis 407 i lab 5 of 7
Cis 407 i lab 5 of 7
helpido9
 
Ôn tập KTTMDT
Ôn tập KTTMDTÔn tập KTTMDT
Ôn tập KTTMDT
mrcoffee282
 
Cis 407 i lab 4 of 7
Cis 407 i lab 4 of 7Cis 407 i lab 4 of 7
Cis 407 i lab 4 of 7
helpido9
 
Implement a Javascript application that allows the user to enter strin.docx
Implement a Javascript application that allows the user to enter strin.docxImplement a Javascript application that allows the user to enter strin.docx
Implement a Javascript application that allows the user to enter strin.docx
mckerliejonelle
 

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

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

Recently uploaded

“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
Muhammad Subhan
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 

Recently uploaded (20)

AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 

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