hello everyone.
can any one tell me how to delete recored from gired view dynamically.
in my program i have one gridview and sqlDatasource in this i have 4 label and one button.Label shows name, date, title and description and button for delete.
all this is done at source view through adding 2 template field and this template field contain label and button. it show recordes as i want.
but the problem is now i want to delete perticuler name and description from the gridview so i had created button in templte field.how to do it can any one sugest me what to do.
thoiis is my query.

Select t1.Id,t1.Name,t1.Date,t1.Description,t2.Body,t2.optionId,t2.AdDate,t2.AdedBy from tabel1 t1 full outer join table2 t2 ON t1.Id=T2.Id

this is my code of gridview

<asp:GridView ID="GridView1"  runat="server" Height="139px" Width="562px" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" HorizontalAlign="Justify" BackColor="LightGoldenrodYellow" BorderColor="Black" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" BorderStyle="Solid">
                                <asp:Literal ID="Literal6" runat="server" Text='<%# Eval("UserDt", "{0:D}<br/><br/>{0:T}") %>'></asp:Literal><br />
                                <hr style="width: 100px; height: 1px" />
                                <asp:Literal ID="Literal7" runat="server" Text='<%# Eval("UserAd", "{0}") %>'></asp:Literal><br />
                                <br />
                                <asp:Button ID="Button2" runat="server" Text="Delete" OnClientClick="if (Confirm('Are you sure you want to delet this{0}?'==false) return false;" />
                        <div class="title">
                            &nbsp;<asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Title", "{0}") %>'></asp:Literal>
                            <br />
                            <br />
                                <asp:Literal ID="Literal5" runat="server" Text='<%# Eval("Body", "{0}") %>'></asp:Literal><br/>
                      <FooterStyle BackColor="Tan" />
                    <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
                    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
                    <AlternatingRowStyle BackColor="PaleGoldenrod" />

here i'm trying to write some code for delete but i don't know what to write.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim id As Integer
            id = GridView1.Rows.Count
            If id = 1 Then
                Exit Sub
            End If
            Dim btn As Button = e.Row.FindControl("button1")
        End If
    End Sub

sorry for my poor english and for this tuck of code but sgest me what to do.

7 Years
Discussion Span
Last Post by prashantchalise

Do you want to delete record from DB after pressing the delete button.? If yes then you can try the following.
1 ) Add appropriate buttons in your aspx page.

	<asp:Button ID="btnDelete" Text="Delete" CommandArgument=''<%#DataBinder.Eval(container,"dataitem.ID")%>'' CommandName="DELETECURRENTROW" OnClientClick="return confirm('Are you sure you want to delete this record?')" runat="server" />

2) Inside your .vb file you should now handle the proper action in following way.

Protected Sub gvTest_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvTest.RowCommand
            Dim ID As Integer = CInt(e.CommandArgument)
            Select Case e.CommandName
                Case "DELETECURRENTROW"
'Execute your Sql for Delete operation. The Record is deleted based on ID generated above.
                    ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "STATUS_MESSAGE", "alert('Record deleted for ID:" & ID & "');", True)
                    'Function BindYourGridView()
            End Select
        End Sub

tahnk for repaly when i run the page and click on delete button i got this error

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Well, There is a script running in the above code. Either set enableEventValidation=false in your aspx page header or remove the script from file.

Also mark this thread as solved when you have your answer. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.