Click here to Skip to main content
15,905,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why selected row is not deleting in gridview ?

my cs code is given below

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        //Loop through all the rows in gridview
        foreach (GridViewRow gvrow in gv1.Rows)
        {
            //Finiding checkbox control in gridview for particular row
            CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
            //Condition to check checkbox selected or not
            if (chkdelete.Checked)
            {
                //Getting email of particular row using datakey value
                //int mail = Convert.ToInt32(gv1.DataKeys[gvrow.RowIndex].Value);
                string email = gv1.DataKeys[gvrow.RowIndex]["mail"].ToString();
                AccessDataSource1.DeleteParameters["mail"].DefaultValue = email;
                AccessDataSource1.Delete();
            }
        }

    }



My html part of gridview is given below

XML
<asp:GridView ID="gv1" runat="server" DataSourceID="AccessDataSource1"
DataKeyNames="mail" CssClass="Gridview" AutoGenerateColumns="False"
HeaderStyle-BackColor="#61A6F8" HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White" AllowPaging="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkdelete" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="cname" HeaderText="NAME" />
<asp:BoundField DataField="dob" HeaderText="DOB" />
<asp:BoundField DataField="city" HeaderText="CITY" />
<asp:BoundField DataField="mail" HeaderText="EMAIL" />
<asp:BoundField DataField="occu" HeaderText="OCCUPATION" />
</Columns>
</asp:GridView>
                        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Delete" />
                        <br />
                        <asp:AccessDataSource ID="AccessDataSource1" runat="server"
                            DataFile="~/Rabiya.mdb"
                            SelectCommand="SELECT [cname], [dob], [city], [mail], [occu] FROM [joining]" DeleteCommand="Delete from joining where mail=@mail">
            <DeleteParameters>
            <asp:Parameter Name="mail" Type="Int32" />
            </DeleteParameters>
                        </asp:AccessDataSource>



My Java Script part is given below

XML
<script type="text/javascript">
    function Confirmationbox() {
        var result = confirm('Are you sure you want to delete selected User(s)?');
        if (result) {
            return true;
        }
        else {
            return false;
        }
    }
</script>
Posted

Hello Mr.janardan
In such kind of issue you firs check where you are binding data to gridview.
check wether you are binding in( if not post back) or out side that.
I think you are binding without checking if not postback.

Mark Answered if it solved you query.
 
Share this answer
 
Check whether you are binding in (if not post back) or outside that.
I think you are binding without checking if not post back.
 
Share this answer
 
v2
Comments
André Kraak 31-Mar-12 8:04am    
Edited solution:
Spelling/Grammar
Removed unnecessary tags

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