Insert, Update and Delete rows in ASP.NET GridView Control
In this Article you can learn how to edit, update, delete, and cancel in gridview.First drag and drop Gridview.In gridview properties set AutoGenarateColumns to False.Next open Default.aspx source code. To make a columns in Gridview use <asp:TemplateField>Here first I created
Introduction
In this Article you can learn how to edit, update, delete, and cancel in gridview.First drag and drop Gridview.In gridview properties set AutoGenarateColumns to False.Next open Default.aspx source code. To make a columns in Gridview use <asp:TemplateField>Here first I created a table name 'Product' in my database.it contains 3 colomns as ProductId,ProductName,ProdcutPrice
1)Download My Sample.
2)Inside that folder person.sql file will be there execute it in your database.
SQL
/****** Object: Table [dbo].[Product] Script Date: 07/07/2012 02:18:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Product]( [pk_id] [int] IDENTITY(1,1) NOT NULL, [ProductId] [varchar](5) NULL, [ProductName] [varchar](50) NULL, [ProductPrice] [numeric](8, 2) NULL, PRIMARY KEY CLUSTERED ( [pk_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO
3)Change the connetion string in web.config file.
C#
<configuration> <connectionStrings> <add name="MyConnection" connectionString="Data Source=MONISH-PC\MONISH;Initial Catalog=master;Persist Security Info=True;User ID=saty;Password=123" providerName="System.Data.SqlClient" /> </connectionStrings>
This sample wrote by three tier architecture so you have to add references.
UI->BusinessLogic
BusinessLogic->DataAccess back to BLBusinessLogic->Commonfunctioon back to BL
BusinessLogic -> UI finally BL return data to UI
Description
In Default.aspx source code we have to add
This is used to Edit the Row in Gridview.Here I am going to Edit only two columns name and marks.
C#
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" > <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField Visible="False"> <ItemTemplate> <asp:Label ID="lblPk_id" runat="server" Text='<%#Eval("pk_id")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField Headertext="ProductId"> <EditItemTemplate> <asp:TextBox ID="txtProductId" runat="server" Text='<%#Eval("ProductId")%>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtProductId" runat="server" Text='<%#Eval("ProductId")%>'></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="lblProductId" runat="server" Text='<%#Eval("ProductId")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ProductName"> <EditItemTemplate> <asp:TextBox ID="txtProductName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtProductName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="lblProductName" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ProductPrice"> <EditItemTemplate> <asp:TextBox ID="txtProductPrice" runat="server" Text='<%#Eval("ProductPrice")%>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtProductPrice" runat="server" Text='<%#Eval("ProductPrice")%>'></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="lblProductPrice" runat="server" Text='<%#Eval("ProductPrice")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> <asp:TemplateField> <FooterTemplate> <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" /> </FooterTemplate> </asp:TemplateField> </Columns> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView>
Source Code Files
- product.sql
Note:I have used SqlServer 2008
For this article we are going to fill GridView by data from database.
We are going to make perform following operations on data using GridView
- Add New record into database; here data will directly be added into database table
- Update Records into database, using edit link.
- Delete Records using delete link
No comments:
Post a Comment