GridView and Entity Framework, deleting a record.

Posted on 2009-05-01
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
    LVL 9

    Accepted Solution

    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

    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

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

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now