Gridview delete button event

I've a gridview with a delete button on each row. When I click the button on a row, I want to delete that row from the Gridviewand from the database. The Gridview is bound to a table of my access database. Below you can find the code of my Gridview.


<asp:GridView ID="grdAbsentismo" runat="server" AutoGenerateColumns="False" DataSourceID="GridDataSource" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="#333333">
                    <asp:BoundField DataField="Empregado" HeaderText="Empregado" SortExpression="Empregado">
                        <ItemStyle HorizontalAlign="Center" Width="150px" />
                    <asp:BoundField DataField="Motivo" HeaderText="Motivo" SortExpression="Motivo">
                        <ItemStyle HorizontalAlign="Center" Width="180px" />
                    <asp:BoundField DataField="MotivoLei" HeaderText="Detalhe Absentismo" SortExpression="MotivoLei">
                        <ItemStyle HorizontalAlign="Center" Width="200px" />
                    <asp:BoundField DataField="DataInicio" HeaderText="Data de Inicio" SortExpression="DataInicio" DataFormatString="{0:dd/MM/yyyy}">
                        <ItemStyle HorizontalAlign="Center" Width="90px" />
                    <asp:BoundField DataField="Dias" HeaderText="N&#186; Dias" SortExpression="Dias">
                        <ItemStyle HorizontalAlign="Center" Width="60px" />
                    <asp:ButtonField ButtonType="Button" Text="Remover">
                        <ItemStyle HorizontalAlign="Center" Width="100px" />
                <HeaderStyle BackColor="#5D7B9D" BorderStyle="Solid" BorderWidth="1px" Height="25px" Font-Bold="True" ForeColor="White" />
                <RowStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Height="25px" BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Left" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <asp:AccessDataSource ID="GridDataSource" runat="server" DataFile="~/db/manpower.mdb"
                SelectCommand="SELECT [Empregado], [Motivo], [DataInicio], [Dias], [MotivoLei] FROM [Absentismo]" CacheExpirationPolicy="Sliding"></asp:AccessDataSource>

Open in new window

LVL 33
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Jorge PaulinoConnect With a Mentor IT Pro/DeveloperCommented:
The easy way (I think) is to remove that buttonfield and add a command field(delete). Then change the "delete" text to "remover" and the button type to "button" (instead of link)
Don't forget you need to have a primary key field!
Obadiah ChristopherCommented:
You'll have to configure a delete command for the accessdatasource
Gyanendra SinghArchitectCommented:
supported to informaniac comment .. need to add this in access datasource
 DeleteCommand="DELETE FROM xxxx
                 WHERE ID=@ID"

also need to add DataKeyNames in your grid control so as per sample it would be
<asp:GridView ID="grdAbsentismo" runat="server" DataKeyNames="ID"  AutoGenerateColumns="False" DataSourceID="GridDataSource" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="#333333">
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

jppintoAuthor Commented:
Can you be more explicit please? I've started using ASP just a few days ago...


Obadiah ChristopherCommented:
jppintoAuthor Commented:
I've tryed the solution presented by jpaulino and it seem to be the simplest solution for what I need to do.


jppintoAuthor Commented:
Simple solution. Good work!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.