GridView Edit

GridView Edit

    In this post I will edit all rows of GridView control. The complete source code can be downloaded from here

GridViewEditAll.zip (3.65 kb)

 

In GridView ItemTemplate we have added two controls, Label control for showing text data and TextBox for editing the data

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">

<Columns>

<asp:TemplateField HeaderText="Sr.No.">

<ItemTemplate>

<%# Container.DataItemIndex+1 %>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="First Name">

<ItemTemplate>

&nbsp;<asp:Label ID="lblFirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:Label><br />

<asp:TextBox ID="txtFirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>' Visible="False"></asp:TextBox>

</ItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Last Name">

<ItemTemplate>

<asp:Label ID="lblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>

<br />

<asp:TextBox ID="txtLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'

Visible="False"></asp:TextBox>

</ItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>

</FooterTemplate>

<EditItemTemplate>

<asp:TextBox ID="txtLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:TextBox>

</EditItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Email">

<ItemTemplate>

<asp:Label ID="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label>

<asp:TextBox ID="txtEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'

Visible="False"></asp:TextBox>

</ItemTemplate>

<FooterTemplate>

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

 

On Edit all link button we are showing the Texboxes and hiding the label controls found in Item Template of GrodView control.


  protected void LinkButton1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox t = (TextBox)gr.FindControl("txtFirstName");
            TextBox tLastName = (TextBox)gr.FindControl("txtLastName");
            TextBox tEmail = (TextBox)gr.FindControl("txtEmail");
            Label lFirstName = (Label)gr.FindControl("lblFirstName");
            Label lLastName = (Label)gr.FindControl("lblLastName");
            Label lEmail = (Label)gr.FindControl("lblEmail");

            if (t.Visible)
            {
                t.Visible = false;
                tLastName.Visible = false;
                tEmail.Visible = false;
                lFirstName.Visible = true;
                lLastName.Visible = true;
                lEmail.Visible = true;
            }
            else
            {
                t.Visible = true;
                tLastName.Visible = true;
                tEmail.Visible = true;
                lFirstName.Visible = false;
                lLastName.Visible = false;
                lEmail.Visible = false;

            }


        }
    }


We can save all edited rows by clicking on Save all link button

 protected void LinkButton2_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            TextBox t = (TextBox)gr.FindControl("txtFirstName");
            TextBox tLastName = (TextBox)gr.FindControl("txtLastName");
            TextBox tEmail = (TextBox)gr.FindControl("txtEmail");
            Label lFirstName = (Label)gr.FindControl("lblFirstName");
            Label lLastName = (Label)gr.FindControl("lblLastName");
            Label lEmail = (Label)gr.FindControl("lblEmail");

            // Call your SQL server Data Access layer to save appropriate fields.

        }

    }

Tags:

GridView EditGridview select emailgridview add new rowasp.net Gridview checkboxJquery Gridview Dropdownlist Selected Change Enable Disable ControlsGridView Rowhow can I edit a pdfAndroid EditText TutorialGridview HyperlinkYouTube Edit Video 500 Internal Server Error Highly Trained MonkeysGridView Serial

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.