How to select & delete record from gridview control

First Create a website in asp.net using c# & design the page 

        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Style="font-family: Arial;|
          
font-size: 10pt" AutoGenerateColumns="false" DataKeyNames="sr_no" Width="222px">
            <Columns
>
                <asp:TemplateField
>
                    <ItemTemplate
>
                        <asp:CheckBox ID="chkdelete" runat="server"
/>
                    </ItemTemplate
>
                </asp:TemplateField
>
                <asp:BoundField DataField="sr_no" HeaderText="Sr.No"
/>
                <asp:BoundField DataField="disease_name" HeaderText="Disease"
/>
            </Columns
>
        </asp:GridView
>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Delete"
/>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shivkamal_webConnectionString %>
"
            SelectCommand="SELECT * FROM [disease_wise_tbl]" DeleteCommand
="delete from disease_wise_tbl where sr_no=@sr_no">
            <DeleteParameters
>
                <asp:Parameter Name="sr_no" Type="Int32"
/>
            </DeleteParameters
>
        </asp:SqlDataSource
>
        after succefully design the page write the code on button click event foreach (GridViewRow
        gvrow in GridView1.Rows) { CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
        if (chkdelete.Checked) { //Getting UserId of particular row using datakey value
        int usrid = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value); SqlDataSource1.DeleteParameters["sr_no"].DefaultValue
        = usrid.ToString(); SqlDataSource1.Delete();
}
}

Ebook Download
View all
Learn
View all