Last post Nov 25, 2008 02:14 PM by bootzilla
Nov 20, 2008 06:05 PM|bootzilla|LINK
I have a Delete field in a grid view. It is a template field. I want to know how I code this to actually delete the row/record from the database table. Here is the aspx grid view code(the field in question is bold):
Nov 20, 2008 07:04 PM|bootzilla|LINK
Any ideas on this would be appreciated.
Nov 20, 2008 07:14 PM|acroitoriu|LINK
Add to the gridview a Rowcommand event handler : OnRowCommand="gridview_RowCommand" and then in the gridview_RowCommand have something like (code is in C# but you'll get the idea [:)] ):
protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
int rowIndex= Convert.ToInt32(e.CommandArgument);
// delete your report from the data source based on the rowIndex
// rebind gridview using gridview.DataSource and gridview.DataBind();
Also: use a buttonfied instead the template field for the button. you can make it as link...
<asp:buttonfield buttontype="Link" commandname="Delete" text="Delete"/>
in this case the you'll use as I mentioned in the code above the rowIndex and not the reportID - you can get your report from the datasource based on the index...
Nov 20, 2008 07:34 PM|bootzilla|LINK
I am using VB, would this code do the job, I'll admit I don't know a lot about how to do this. The aspx code is first and then the code behind:
Nov 21, 2008 01:16 PM|bootzilla|LINK
I still need help from someone please.
Nov 25, 2008 06:42 AM|Qin Dian Tang - MSFT|LINK
Use the code from your original post: <asp:LinkButton
CommandArgument='<%# Eval("reportid") %>'
Handle RowCommand event of GridView:
Protected Sub MonthlyDownloadsGrid_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName & "Delete" Then
Dim reportid As String = e.CommandArgument.ToString()
Dim conn As New SqlConnection("Server=(local)\SQLEXPRESS;Integrated Security=SSPI;Database=test;Persist Security Info=True")
Dim cmd As New SqlCommand("delete [table] where reportid=" & reportid, conn)
This is just my sample. The point is to get reportid from that row and use this id to delete from table.
Nov 25, 2008 02:14 PM|bootzilla|LINK