Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I would like to know how to use the Delete Command to delete records from a table in a database through GridView.

This is what I did so far, with no avail.

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" ViewStateMode="Enabled">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="content_name" HeaderText="Content Name" SortExpression="content_name">
            </asp:BoundField>
            <asp:BoundField DataField="content_type" HeaderText="Content Type" SortExpression="content_type">
            </asp:BoundField>
            <asp:CommandField ShowSelectButton="True">
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:CommandField>
            <asp:CommandField ShowDeleteButton="True">
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:CommandField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>


XML
<asp:SqlDataSource ID="SqlDataSource1"
         runat="server"
         ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>"
         SelectCommand="SELECT [content_name], [content_type] FROM [content]"
         DeleteCommand="DELETE content WHERE content_id=@content_id"
        >



VB
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand


        If e.CommandName.Equals("Delete") Then

            Dim rowID As Integer = GridView1.SelectedRow.RowIndex
            SqlDataSource1.InsertParameters.Add("@content_id", rowID)


        End If

    End Sub
End Class
Posted
Comments
Richard C Bishop 11-Apr-13 9:54am    
Honestly, unless you have a specific requirement not to do so, just use the smart tag of the gridview and datasource control to set that up. You can automatically enable selecting and deleting.
Member 9978591 11-Apr-13 9:59am    
I tried that but it didn't work. That's how it currently is.. I added the 'Delete' command from 'Edit Columns'. The error: Must declare the scalar variable "@content_id".

The problem seems the way you check the row that was selected for deletion.
Also I cannot see the Content_ID as a bound field to be passed or retrieved in code behind.
Should be defined before Content_Name.



If e.CommandName.Equals("Delete") Then

            Dim rowID As Integer = Convert.ToInt32(e.CommandArgument)

            GridView1.DeleteRow (rowID)

            

        End If
 
Share this answer
 
v4
Comments
Member 9978591 11-Apr-13 10:51am    
@Ashwin_Nikam That gave me en error: Must declare the scalar variable "@content_id".
Member 9978591 11-Apr-13 10:51am    
I had to convert the code to VB.NET


Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand

If e.CommandName.Equals("Delete") Then

Dim rowID As Integer = Convert.ToInt32(e.CommandArgument)

Dim GvRowSelected As GridViewRow = GridView1.Rows(rowID)

SqlDataSource1.InsertParameters.Add("@content_id", GvRowSelected.Cells(1).Text)

End If

End Sub
Ashwin_Nikam 11-Apr-13 11:36am    
ok..please check if follwoing works

If e.CommandName.Equals("Delete") Then

Dim rowID As Integer = Convert.ToInt32(e.CommandArgument)

GridView1.DeleteRow (rowID)



End If
write your code to delete in rowdeleting event of gridview
 
Share this answer
 

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