GridView Edit Delete using SqlDataSource
SqlDataSource is a data control can be used to manage data such as edit, delete. With this article you may find it easier to maintain the data with SqlDataSource. This code doesn’t need any code behind code to edit or delete. And moreover wizards can be used to generate the Markup.
CommandField for enabling GridView Edit
For firing edit and delete commands we need a command button on each row. With the help of GridView CommandField it is much easier to get the command buttons on each row. The following two properties will bring the edit and delete button to the GridView.
ShowEditButton="True" will show the edit button. ShowDeleteButton="True" will show the delete button.
You may leave the Auto Generate columns to true, but I liked to use the BoundField to define the columns to be shown. If you wish to make any column non editable then the ReadOnly="True" prevent bring the column to edit mode.
SqlDataSource and SQL Commands
Using the wizard or manually configuring the SQL connection will give the Connection String (you can find the entry in Web.Config under connection strings)
Like above, the commands also can be created in the wizard or can be manually written in the SqlDataSource.
Once all the commands are created, the next step is to create the respective parameters for the created commands. All the parameters have to be given for the commands Delete, Edit, Insert and select. If the commands are wrong then the GridView Edit may not work Properly.
Source Code
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="GridViewEditDelete._Default" %>
<!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" AutoGenerateColumns="False" DataKeyNames="RegionID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="RegionID" HeaderText="RegionID" ReadOnly="True" />
<asp:BoundField DataField="RegionDescription" HeaderText="RegionDescription"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM Region where RegionId = @RegionId"
InsertCommand="INSERT INTO Region( RegionID, RegionDescription) VALUES (@RegionID, @RegionDescription)"
SelectCommand="SELECT [RegionID], [RegionDescription] FROM [Region]"
UpdateCommand="UPDATE Region SET RegionDescription = @RegionDescription where RegionID = @RegionID">
<DeleteParameters>
<asp:Parameter Name="RegionId" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="RegionDescription" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="RegionID" />
<asp:Parameter Name="RegionDescription" />
</InsertParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
No code behind required for this sample GridView Edit