Some of the most basic operations that we perform using the GridView control is update and delete. As it so happens these operations are also very easy to implement. You don't have to write a single line of code for update and delete. In this article we will see that how you can implement these functions.

 

Introduction:

Some of the most basic operations that we perform using the GridView control is update and delete. As it so happens these operations are also very easy to implement. You don't have to write a single line of code for update and delete. In this article we will see that how you can implement these functions.

Creating a Data Source:

You can use the SqlDataSource object to create a data source for the GridView control. SqlDataSource is very easy to use just drag and drop it on the web form and a wizard will guide you on through the steps.

Implementing Update and Delete functionality in GridView:

First you need to set the SqlDataSource UpdateCommand and its parameters. This is very simple just go to the html view and write your update command. You can also include parameters using design view using design view sometimes includes special characters in your query which makes it hard to read. Anyway, PersonID is the primary key and name is the column that we are updating. If you notice I have used @orginal_PersonID and not @PersonID. The reason is GridView needs to know the original primary key through which it can update the data. If you don't prefix it with @original than there is no way to find that which row is updated.  

<asp:SqlDataSource ID="mySource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

SelectCommand="SELECT [PersonID], [Name] FROM [tblPerson]" UpdateCommand="UPDATE tblPerson SET Name = @Name WHERE PersonID = @original_PersonID"

DeleteCommand = "DELETE FROM tblPerson WHERE PersonID = @original_PersonID">

<UpdateParameters>

<asp:Parameter Name="@Name" />

<asp:Parameter Name="@PersonID" />

</UpdateParameters>

</asp:SqlDataSource>

 

You must set the DataKeyNames property to the primary key of your table. AutoGenerateEditButton and AutoGenerateDeleteButton will place edit and delete buttons in the GridView control.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None"

BorderWidth="1px" CellPadding="4" DataKeyNames="PersonID" DataSourceID="mySource" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateSelectButton="True">

<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

<RowStyle BackColor="White" ForeColor="#330099" />

<Columns>

<asp:CommandField ShowSelectButton="True" />

<asp:BoundField DataField="PersonID" HeaderText="PersonID" InsertVisible="False"

ReadOnly="True" SortExpression="PersonID" />

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

</Columns>

<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

</asp:GridView>

 

That's basically it. So, you see how simple it is to update and delete data using GridView control.

I hope you like the article, happy coding!