Tuesday 4 March 2014

ASP.NET Delete data in GridView

In this article I will explain How to Delete GridView Data in ASP.NET.

Below design code and aspx.cs code is useful for Deleting records to Database(Delete Data into DB) by using ASP.NET Gridview.

The GridView control is a feature rich and versatile control used to accept, display, and edit data on a web page. It is a commonly used control in ASP.Net web applications. 

To use a GridView control a DataSource control has to be attached to the GridView control. The property DataSourceID of the GridView control binds the GridView control to the DataSource control and allows paging, sorting and database operations with the DataSource. 

ASPX GridVive design :




<asp:GridView ID="gvContact" runat="server" AutoGenerateColumns="false"
                OnRowCommand="gvContact_RowCommand" OnRowEditing="gvContact_RowEditing"
                AllowPaging="true" PageSize="20" OnRowCancelingEdit="gvContact_RowCancelingEdit"
                OnRowDataBound="gvContact_RowDataBound"
                OnRowDeleting="gvContact_RowDeleting">
                <Columns>
                    <asp:TemplateField HeaderText="Operations">
                        <ItemTemplate>
                            <span>
                                <asp:LinkButton Text="Edit" ID="Edit" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                    CommandName="Edit" ToolTip="Edit" />&nbsp;&nbsp;                                   
                                        <asp:LinkButton ID="lnkBDelete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
                            </span>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:LinkButton Text="Update" ID="Update" ForeColor="Red" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                CommandName="Update" ToolTip="Update" ValidationGroup="Update" />&nbsp;&nbsp;<asp:LinkButton
                                    Text="Cancel" ID="Cancel" ForeColor="Red" runat="server" CommandArgument="<%# Container.DataItemIndex %>"
                                    CommandName="Cancel" ToolTip="Cancel" />
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:LinkButton runat="server" ID="btnAdd" Text="Add" CommandArgument="<%# Container.DataItemIndex %>"
                                CommandName="Add" ToolTip="Add" ValidationGroup="Add" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Main No" HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Label ID='lblMainNo' runat="Server" Text='<%# Eval("MainNo") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID='txtMainNoET' runat="Server" Width="150px"></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID='txtMainNoFT' runat="Server" Width="150px">
                            </asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name" HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Label ID='lblName' runat="Server" Text='<%# Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID='txtNameET' runat="Server" Width="150px"></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID='txtNameFT' runat="Server" Width="150px">
                            </asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Email" HeaderStyle-CssClass="CenterAlign" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Label ID='lblEmail' runat="Server" Text='<%# Eval("Email") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID='txtEmailET' runat="Server" Width="150px"></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID='txtEmailFT' runat="Server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>


ASPX.CS CODE FOR RECORD DELETING PURPOSE:

Below code is useful for Asking conformation message for record delete or not?

protected void grdTCMMain_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnDelete = (LinkButton)e.Row.FindControl("lnkBDelete");
            if (btnDelete != null)
            {
                btnDelete.Attributes.Add("onclick", "javascript:return " +
                    "confirm('Are you sure you want to delete this record?')");               
            }
        }
    }

If the user clicked on ok button then the  below code is executed and Deleted the record into DB.

protected void gvContact_RowCommand(object sender, GridViewCommandEventArgs e)
    {
if (e.CommandName == "Delete")
        {
            DataTable dtDeleteMessage = new DataTable();

            int deleteRowID = Convert.ToInt32(e.CommandArgument);

            int PersonSk = Convert.ToInt32(gvContact.DataKeys[(deleteRowID) - (this.gvContact.PageIndex * 20)].Value.ToString());

            objclass.ActionTender = 3;
            objclass.PersonSk = PersonSk;
            dtDeleteMessage = objclass.DeleteInfo();

            if ((dtDeleteMessage == null))
            {
                gvContact.EditIndex = -1;
                gvContact.DataSource = dtDeleteMessage;
                gvContact.DataBind();
                string message = "Data Deleted successfully !";
                string script = "<script language=\"javascript\"  type=\"text/javascript\">;alert('" + message + "');</script>";
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "AlertMessage", script, false);
            }
            else
            {

            }
        }
}



No comments:

Post a Comment