Select, Update, Delete in GridView in ASP.NET

Select, Update, Delete in GridView in ASP.NET


Posted in : ASP.Net Posted on : December 8, 2010 at 4:00 PM Comments : [ 0 ]

In this article we will introduce with how can we Select, Update, Delete data items in GridView in ASP.NET

Select, Update, Delete in GridView in ASP.NET

GridView in ASP.NET is used to display data items in Table format. GridView control just like a DataGrid. GridView has more functionality like selecting, editing, paging and sorting data items. In this example we use SqlDataSource for providing data source to GridView. In this example we use SqlDataSource for providing data source and selecting, editing, deleting data items in GridView. You will click on smart tag of GridView and click on paging, sorting, editing sorting and deleting. You can see the following image.

GridViewSelectUpdateDelete.aspx (Design Page):

GridViewSelectUpdateDelete.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="GridViewInsertUpdateDelete.aspx.cs" 
Inherits="GridViewInsertUpdateDelete" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color:Green">GridView Select, Update, Delete in ASP.NET 4, C#</h2>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
AutoGenerateColumns="False" BorderColor="#CC9966" BorderStyle="None" 
BorderWidth="1px" CellPadding="4" DataKeyNames="ProductID" 
DataSourceID="SqlDataSource1" Font-Bold="True" Font-Names="Verdana" 
Font-Size="Small" PageSize="5" BackColor="White">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
ShowSelectButton="True" />
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" 
SortExpression="ProductName" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ChartDatabaseConnectionString %>" 
DeleteCommand="DELETE FROM [Product] WHERE [ProductID] = @ProductID" 
InsertCommand="INSERT INTO [Product] ([ProductID], [ProductName], [Price]) 
VALUES (@ProductID, @ProductName, @Price)" 
SelectCommand="SELECT * FROM [Product]" 
UpdateCommand="UPDATE [Product] SET [ProductName] = @ProductName, 
[Price] = @Price WHERE [ProductID] = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="Price" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="Price" Type="String" />
<asp:Parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</asp:Content>

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics