In-place Updating and Deleting in GridView
By AzamSharp
Views: 14537

 

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!

 

 

By AzamSharp




Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Edit GridView
Name: Vic
Date: 3/25/2007 5:44:34 PM
Comment:
Once again, good sample.
Subject: Good One
Name: Deepak Kashyap
Date: 6/14/2007 9:27:13 PM
Comment:
Your article is good one.
Thanks and keep it up!
Subject: gridview
Name: jvchoudary
Date: 8/29/2007 2:48:05 AM
Comment:
if we can use more than one sqldatasource to grid view
Subject: Update
Name: Richard
Date: 10/4/2007 7:50:09 AM
Comment:
How would I use a temptable if using a stored procedure in oracle? What would be the Datasource OracleDataSource?
Subject: RE: Update
Name: AzamSharp
Date: 10/7/2007 9:20:15 AM
Comment:
Hi Richard,

You can use OracleDataSource. I have not tried OracleDataSource yet!
Subject: Missed a step?
Name: Tim the Enchanter
Date: 2/4/2008 8:41:33 AM
Comment:
Can help feel that you have missed a step... how do you marry up @original_PersonID as a parameter with the UPDATE command?
Subject: Warning: SqlDataSource
Name: Tim the Enchanter
Date: 2/4/2008 9:07:25 AM
Comment:
Nothing happening?

OldValuesParameterFormatString should be set to "original_{0}" by default. In my case it was not and I have not changed anything from the defaults.
Subject: delete command
Name: vineet thakur
Date: 3/5/2008 4:32:22 AM
Comment:
This article help me in gridview . the way of demo was too good
Subject: Deleting with Bound Datasource not connected with a database
Name: Daniel
Date: 6/3/2008 1:31:29 PM
Comment:
This is an excellent article and webpage, however I cannot find an answer to my issue. I've got a Dataset compiled from a SQL query bound to a GridView that can allow deletions but due to the type of data being displayed it's unnecessary for any sql queries to be run during the deletion. I find my row index via the command argument passed from the button press, but when I call GridView.DeleteRow(index) I get nothing. There's no error, but after running the value is still being displayed to the screen. Is there a way to remove the value from the dataset and have the GridView redisplay in a similar manner to your In-place method here?
Subject: RE: Deleting with Bound Datasource not connected with a database
Name: AzamSharp
Date: 6/3/2008 4:19:24 PM
Comment:
Hi Daniel,

So, basically you are only interested in removing the row from the dataset and not the database? Once you remove the row from the dataset are you assigning the dataset back to the GridView control and binding it using GridView1.DataBind()?





Join WebHost4Life.com







Copyright GridViewGuy 2007-2008