Blog author:
Devashishjangid831989 | Posted on: 6/17/2012 | Category:
ASP.NET Blogs | Views: 1041 | Status:
[Member] |
Points: 75
|
Alert Moderator
Introduction:-
In this example we can Insert,Update,Delete the data or Information about a person at runtime with the help of gridview.
Example:-
The picture above shows an Gridview at runtime and we need to insert,update and delete the data in data base by this...
How To Work This:-
Step 1 : Create the table into database like this..
OR
:Add the following code in sqlserver2008 and run
create table employe
(
id int constraint pk primary key identity(1,1),
First_Name varchar(50),
Last_Name varchar(50),
Department varchar(50),
Location varchar(50)
)
Step 2 : Create Your (.aspx) file by paste this code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditInsertDeleteInGridview.aspx.cs" Inherits="EditInsertDeleteInGridview" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="id"
ShowFooter="true"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
Width="459px" OnRowCommand="GridView1_RowCommand" PageSize="5"
Height="266px" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
<asp:TemplateField HeaderText="Serial No.">
<ItemTemplate>
<%# Container.DataItemIndex +1 %>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" SortExpression="id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="First_Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("First_Name") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("First_Name") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFname" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("Last_Name") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%#Bind("Last_Name") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLname" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department" SortExpression="Department">
<ItemTemplate>
<asp:Label ID="lblDepartment" runat="server" Text='<%#Eval("Department") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDepartmentName" runat="server" Text='<%#Bind("Department") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDept" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLocation" runat="server" Text='<%#Bind("Location") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLoc" runat="server">
</asp:TextBox>
</FooterTemplate>
</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>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:adessysConnectionString2 %>"
SelectCommand="SELECT [id], [First_Name], [Last_Name], [Department], [Location] FROM [employe]"
DeleteCommand="DELETE FROM [Employe] WHERE [id] = @ID"
InsertCommand="INSERT INTO [Employe] ([First_Name],[Last_Name],[Department], [Location])VALUES (@First_Name, @Last_Name, @Department, @Location)"
UpdateCommand="UPDATE [Employe] SET [First_Name] = @First_Name, [Last_Name] = @Last_Name,[Department] = @Department, [Location] = @Location WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32"/>
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="First_Name" Type="String" />
<asp:Parameter Name="Last_Name" Type="String" />
<asp:Parameter Name="Department" Type="String" />
<asp:Parameter Name="Location" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="First_Name" Type="String" />
<asp:Parameter Name="Last_Name" Type="String" />
<asp:Parameter Name="Department" Type="String" />
<asp:Parameter Name="Location" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<br />
</div>
</form>
</body>
</html>
This code is looks like our example...
Step 3 :Create Your (aspx.cs) file by paste this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class EditInsertDeleteInGridview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
string strFirstName = ((TextBox)
GridView1.FooterRow.FindControl("txtFname")).Text;
string strLastName =
((TextBox)GridView1.FooterRow.FindControl
("txtLname")).Text;
string strDepartment =
((TextBox)GridView1.FooterRow.FindControl
("txtDept")).Text;
string strLocation = ((TextBox)GridView1.FooterRow.
FindControl("txtLoc")).Text;
SqlDataSource1.InsertParameters["First_Name"].DefaultValue
= strFirstName;
SqlDataSource1.InsertParameters["Last_Name"].DefaultValue
= strLastName;
SqlDataSource1.InsertParameters["Department"].DefaultValue
= strDepartment;
SqlDataSource1.InsertParameters["Location"].DefaultValue
= strLocation;
SqlDataSource1.Insert();
}
}
}Step 4: Run the project Insert,Update,Delete With the help of Gridview.
Press F5 to run the project, you can see picture:
Editing:-
screen 1:-
screen 2:-By clickine the Edit Button
screen 3:-Changing the data
screen 4:- click on Update Button...and...See the result
Note:-
1.Just like update the record or data we can aslo edit the new row by entering the data in footer textbox's and click on insert button.
2.just like insert and update the record,if we want to dalete the records from gridview it's done by just clicking on Delete button.
3.Here we can use the concept of paging to show the records in the gridview.
Dev Ashish Jangid
Found interesting? Add this to: