Tuesday 4 March 2014

ASP.NET Update data in GridView


In this article I will explain How to Update GridView EditItemTemplate (Footer Template) Data in ASP.NET.

Below design code and aspx.cs code is useful for Updating records to Database(Update into DB) by using ASP.NET Gridview EditItemTemplate.

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. 

First of all we need to design Edit Item template for new records Updating purpose. Below is the code for page design.



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 UPDATING PURPOSE: 

protected void gvContact_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            DataTable dtuPdATEMessage = new DataTable();
          
            TextBox txtMainNoET = this.gvContact.Rows[this.gvContact.EditIndex].FindControl("txtMainNoET") as TextBox;
            TextBox txtNameET = this.gvContact.Rows[this.gvContact.EditIndex].FindControl("txtNameET") as TextBox;
            TextBox txtEmailET = this.gvContact.Rows[this.gvContact.EditIndex].FindControl("txtEmailET") as TextBox;

            objclass.ActionTender = 2;
            objclass.MainNo = txtMainNoET.Text;
            objclass.Name = txtNameET.Text;
            objclass.Email = txtEmailET.Text;
            dtuPdATEMessage = objclass.AddInfo();

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

            }
           
        }

        if (e.CommandName == "Cancel")
        {
            gvContact.EditIndex = -1;
            dtADDMessage = objclass.AddInforGrid();
            gvContact.DataSource = dtADDMessage;
            gvContact.DataBind();
        }
}


No comments:

Post a Comment