Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


GridView and Entity Framework, deleting a record.

Posted on 2009-05-01
Medium Priority
Last Modified: 2013-11-08
I am using a Gridview with a datasource set to a Entity (using Entity Framework), and Im getting a bit stumped by the process by which one would delete a record from the gridview. I am using a simple asp:LinkButton with the CommanName="delete" which fires the RowDeleting event on the gridview. However I do not understand from there how I am supposed to associate the row to to the entity to delete the correct record. I put a hidden bound field with the primary key for each record in the GridView, as a step towards a solution but Im confused as to what to do from here. I cant seem to find much information about this combination online, so I figured I would ask here. Please let me know if you require any more information. I will post some of the code in the code view for your reference.
<asp:GridView ID="ActionGrid" runat="server"
            <asp:BoundField DataField="action_ID" Visible="false" />
                    <asp:LinkButton runat="server"  CommandName="delete">[x]</asp:LinkButton>
     Protected Sub Row_delete(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles ActionGrid.RowDeleting
     End Sub

Open in new window

Question by:Zen_Dragon
  • 2

Accepted Solution

tillgeffken earned 2000 total points
ID: 24284082
CommandArgument to the rescue..

Assuming that action_Id is your PK.

Delete the record and databind the gridview again

<asp:LinkButton runat="server" CommandArgument="'<%# Eval("action_Id") %> CommandName="delete">[x]</asp:LinkButton>

Open in new window


Author Comment

ID: 24284130
Buddy here at worked helped me figure this out so I figured I would post it up here for everyone else. I ended up using the RowCommand event instead with a custom command.
		If e.CommandName = "deleteAction" Then
			If ActionGrid.Rows.Count <= 1 Then
				MsgBox("You must keep at least one record.")
			End If
			'Delete the action
			Dim actionId As Integer = Convert.ToInt32(e.CommandArgument)
			Using context As New TCDEntities
				Dim _action = (From a In context.action _
							 Where a.action_ID = actionId _
							 Select a).FirstOrDefault()
				If _action IsNot Nothing Then
				End If
			End Using
			'Rebind the grid
		End If

Open in new window


Author Closing Comment

ID: 31577082
Yea thats basically what I ended up doing... see my additional comments for the additional code that I had to add.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question