Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
i have link button name "delete" in gridview:
when i click on any row delete button that row delete from gridview

and i use datatable to bind gridview


i dont knw how to do it ...
Posted

I think you need something like this one:

ASP.Net GridView - Delete Row with Confirmation
[^]
 
Share this answer
 
Comments
anurag19289 9-Nov-13 15:15pm    
nice..thank you
ridoy 9-Nov-13 15:26pm    
Thank you.
priyanshbhaliya 9-Nov-13 22:57pm    
i try this but i dont work.... in my case
ridoy 10-Nov-13 1:53am    
So share your code here, or find the error and tell about that.
priyanshbhaliya 10-Nov-13 8:04am    
this is my code:
gridview:
<asp:GridView ID="gvSavedetails" runat="server" >
<columns>
<asp:TemplateField>
<itemtemplate>
<asp:LinkButton ID="lnkDelete" CausesValidation="false" CommandArgument='<%#Eval("cust_id") %>' CommandName="Del" runat="server" OnClientClick="return confirm('Are you sure you want to delete this Customer?');">
<img src="Image/delete.jpg" alt="delete" />








on rowcommand event:
Protected Sub gvSavedetails_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvSavedetails.RowCommand

If e.CommandName = "Del" Then

Dim dt As DataTable = TryCast(ViewState("add"), DataTable)
Dim dr As DataRow
For Each dr In dt.Rows
If (dr("cust_id").ToString = e.CommandArgument) Then
dr.Delete()
End If
dt.AcceptChanges()
Next

gvSavedetails.DataSource = dt
gvSavedetails.DataBind()
ViewState("add") = dt
End If


End Sub

error come on foreach loop:
"Collection was modified; enumeration operation might not execute."
i just change foreach to for loop and its done...
VB
If e.CommandName = "Del" Then

    Dim dt As DataTable = TryCast(ViewState("add"), DataTable)

    For i As Integer = 0 To dt.Rows.Count - 1
        If (e.CommandArgument = dt.Rows(i)("cust_id").ToString) Then
            dt.Rows.Remove(dt.Rows(i))
        End If
    Next
    gvSavedetails.DataSource = dt
    gvSavedetails.DataBind()
    ViewState("add") = dt
End If



thnks to all commentates ...for helping..
 
Share this answer
 
Comments
CHill60 10-Nov-13 8:32am    
It's important that you understand the original problem so just to wrap this up for you, the reason you got the error on your first attempt was because the collection of rows referred by the For Each was altered by removing an item from the collection. The way that "gap" would be treated cannot then be guaranteed - hence the message.
Similarly in your new approach ... if you have an item with index 3 and delete it, is the next item now at index 3 or still at index 4?
Depending on the language/compiler etc you might get different results - and they can sometimes be very difficult to debug!
I usually get around this problem by starting my loop at the END of the collection and moving towards the start - that way the index of the "next" item will always be assured. I.e. I would use For i As Integer = dt.Rows.Count - 1 to 0 Step -1
priyanshbhaliya 10-Nov-13 8:46am    
thnks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900