This is a migrated thread and some comments may be shown as answers.

How Would i delete a record from a database using a hyperlink in a RadGridView

3 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Goat_
Top achievements
Rank 1
Goat_ asked on 26 Apr 2013, 07:29 AM
Hi guys,

I currently have a RadGridView that populates data from a SQL database, i have added a 'telerik:GridTemplateColumn' with a Delete hyperlink inside of it.

<telerik:GridTemplateColumn UniqueName="TemplateDeleteColumn">
<ItemTemplate>
<asp:HyperLink ID="DeleteLink" runat="server" Text="Delete"></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>

What happens is, when the gridView populates every row now has a delete link in it. How would i go about using C#  to delete that specific record from the SQL database

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 26 Apr 2013, 08:00 AM
Hi,

Try using Asp:LinkButton instead of the hyperlink and add CommandName as 'Delete' so that DeleteComand event will fire .

ASPX:
<telerik:GridTemplateColumn UniqueName="TemplateDeleteColumn">
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
    </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
   //your code to access Database and delete
}

Thanks,
Princy.


0
Goat_
Top achievements
Rank 1
answered on 27 Apr 2013, 05:36 PM
Thank you that works but now im stuck with actually updating a record from my database where the row in the gridview is equal to the record in my database

so for example this code works

protected void grdIncidents_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            iThNkPOCO.Context.iThNkContext db = new iThNkPOCO.Context.iThNkContext();
 
            var cmd = (from i in db.Incidents
                       where i.IncidentID == 8  // What must I place here instead of the 8 ??
                       select i).FirstOrDefault();
 
            cmd.Active = false;
 
            db.SaveChanges();
        }


but then no matter which delete Button i press it will only set the Incident to inactive where the IncidetID = 8, how do i get the IncidentID for the row i am clicking the Delete Button on?

and how would i place that into my C# code?
0
Princy
Top achievements
Rank 2
answered on 29 Apr 2013, 03:45 AM
Hi,

Please try setting the DataKeyName in the MasterTableView as 'IncidentID' and you can retrieve the DataKeyValue of the row you wish to delete and delete the row in DataBase matching with the same DataKeyName. Please take a look into the following code snippet for UpdateCommand.

C#:
protected void grdIncidents_UpdateCommand(object sender, GridCommandEventArgs e)
{
         
        GridEditableItem editItem = (GridEditableItem)e.Item;
        string IncidentIDKey = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["IncidentID"].ToString();
        //OR
        //convert to integer if you want
        int IncidentIDKey = Convert.ToInt32(editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["IncidentID"].ToString());
 
        iThNkPOCO.Context.iThNkContext db = new iThNkPOCO.Context.iThNkContext();
        var cmd = (from i in db.Incidents
                       where i.IncidentID == IncidentIDKey 
                       select i).FirstOrDefault();
  
        cmd.Active = false;
  
        db.SaveChanges();
         
}

Thanks,
Princy.
Tags
Grid
Asked by
Goat_
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Goat_
Top achievements
Rank 1
Share this question
or