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.

        }

    }

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Author

code tutorial