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):
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 [:)] ):
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...
Good luck!
Andrei
http://www.croitoriu.com
"No complexity beyond what is necessary"
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:
<
asp:ButtonField
ButtonType=
"Link"
CommandName="Delete"
Text="Delete"
/>
Protected
Sub gridview_RowCommand(ByVal sender
As
Object,
ByVal e
As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowIndex =
"Delete"
Then
Dim rowIndex = Convert.ToInt32(e.Row)
End
If
End
Sub
Use the code from your original post: <asp:LinkButton
ID="LinkButton1"
CommandArgument='<%# Eval("reportid") %>'CommandName="Delete"
runat="server">Delete</asp:LinkButton>
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)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End If
End Sub
This is just my sample. The point is to get reportid from that row and use this id to delete from table.
bootzilla
Member
137 Points
597 Posts
How to delete row in grid view in VB.net?
Nov 20, 2008 06:05 PM|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):
<asp:GridView ID="MonthlyDownloadsGrid" BorderWidth="2pt" BorderStyle="Double" BorderColor="Black" runat="server" AutoGenerateColumns="False" Width="728px"> <Columns> <asp:BoundField HeaderStyle-BorderColor = "Black" HeaderStyle-BorderStyle ="Inset" ItemStyle-BorderColor = "Black" ItemStyle-BorderStyle="Inset" DataField="title" HeaderText="Title" /> <asp:BoundField HeaderStyle-BorderColor = "Black" HeaderStyle-BorderStyle ="Inset" ItemStyle-BorderColor = "Black" ItemStyle-BorderStyle="Inset" DataField="date" DataFormatString="{0:dd/mm/yyyy}" HeaderText="Date Uploaded" /> <asp:BoundField HeaderStyle-BorderColor = "Black" HeaderStyle-BorderStyle ="Inset" ItemStyle-BorderColor = "Black" ItemStyle-BorderStyle="Inset" DataField="reportmonth" HeaderText="Month For" /> <asp:TemplateField HeaderStyle-BorderColor = "Black" HeaderStyle-BorderStyle ="Inset" ItemStyle-BorderColor = "Black" ItemStyle-BorderStyle="Inset" HeaderText="File Type"> <ItemTemplate> <asp:HyperLink ID="hlThumbnails" runat="server" ImageUrl="images/reports/RepDown_pdficon.gif" NavigateUrl='<%# Eval("finalpath") %> ' Text='<%# Eval("finalpath") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText=""> <ItemTemplate> <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("reportid") %>' CommandName="Delete" runat="server"> Delete</asp:LinkButton></
ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>bootzilla
Member
137 Points
597 Posts
Re: How to delete row in grid view in VB.net?
Nov 20, 2008 07:04 PM|LINK
Any ideas on this would be appreciated.
acroitoriu
Participant
969 Points
168 Posts
Re: How to delete row in grid view in VB.net?
Nov 20, 2008 07:14 PM|LINK
Hi,
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 [:)] ):
{if(e.CommandName.Equals("Delete")){"No complexity beyond what is necessary"
bootzilla
Member
137 Points
597 Posts
Re: How to delete row in grid view in VB.net?
Nov 20, 2008 07:34 PM|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:
<
asp:ButtonField ButtonType= "Link" CommandName="Delete" Text="Delete" /> Protected Sub gridview_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowIndex = "Delete" Then Dim rowIndex = Convert.ToInt32(e.Row) End If End Subbootzilla
Member
137 Points
597 Posts
Re: How to delete row in grid view in VB.net?
Nov 21, 2008 01:16 PM|LINK
I still need help from someone please.
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: How to delete row in grid view in VB.net?
Nov 25, 2008 06:42 AM|LINK
Hi bootzilla,
Use the code from your original post: <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("reportid") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
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)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End If
End Sub
This is just my sample. The point is to get reportid from that row and use this id to delete from table.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
bootzilla
Member
137 Points
597 Posts
Re: How to delete row in grid view in VB.net?
Nov 25, 2008 02:14 PM|LINK
Thanks