Update Record in Gridview with Naming Container in Asp.net using c#

Posted by Unknown On Tuesday, 19 March 2013 0 comments
First desing the page like following way


<table border="2">
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Product Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Product Name Required" Text="*" ControlToValidate="TextBox1" ValidationGroup="val1"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter valid product name" ControlToValidate="TextBox1" ValidationExpression="[a-zA-Z ]+" ValidationGroup="val1"></asp:RegularExpressionValidator>
            </td>

            <td>
                <asp:Label ID="Label2" runat="server" Text="Description"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Product Description Required" Text="*" ControlToValidate="TextBox2" ValidationGroup="val1"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Enter valid description" ControlToValidate="TextBox2" ValidationExpression="[a-zA-Z ]+" ValidationGroup="val1"></asp:RegularExpressionValidator>   
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="Quantity"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server" MaxLength="5"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Quantity Required" Text="*" ControlToValidate="TextBox3" ValidationGroup="val1"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Enter only number" ControlToValidate="TextBox3" ValidationExpression="[0-9.]+" ValidationGroup="val1"></asp:RegularExpressionValidator>
               
            </td>
            <td>
                <asp:Label ID="Label4" runat="server" Text="Rate"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server" MaxLength="5"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Rate Required" Text="*" ControlToValidate="TextBox4" ValidationGroup="val1"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Enter Only Number" ControlToValidate="TextBox4" ValidationExpression="[0-9.]+" ValidationGroup="val1"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" ValidationGroup="val1" />
            </td>
            <td colspan="2">
                <asp:Button ID="Button2" runat="server" Text="Reset" onclick="Button2_Click" />
            </td>
        </tr>
    </table>
    <br />
    <br />
    <br />
   
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
        onrowdeleting="GridView1_RowDeleting"
        onrowdatabound="GridView1_RowDataBound" ShowFooter="True"
        AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging"
        PageSize="3">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="Label5" runat="server" Text="Product Name"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%#Eval("pname") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="Label7" runat="server" Text="Description"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label8" runat="server" Text='<%#Eval("pdesc") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="Label9" runat="server" Text="Quantity"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label10" runat="server" Text='<%#Eval("qty") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="Label14" runat="server" Text="Label"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="Label11" runat="server" Text="Rate"></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label12" runat="server" Text='<%#Eval("rate") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="Label13" runat="server" Text=""></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/edit.jpeg"
                        Width="45px" Height="45px" onclick="ImageButton1_Click" ToolTip="Edit"/>
                    <asp:ImageButton ID="ImageButton2" runat="server"
                        ImageUrl="~/images/delte.jpeg" Width="45px" Height="45px"
                        onclick="ImageButton2_Click" ToolTip="Delete"/>
                </ItemTemplate>
            </asp:TemplateField>
           
        </Columns>
    </asp:GridView> 

Now Write the code at behind in .aspx.cs file

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton lb = (ImageButton)sender;
        GridViewRow rw = (GridViewRow)lb.NamingContainer;

        Label name = (Label)rw.FindControl("Label6");
        Label desc = (Label)rw.FindControl("Label8");
        Label q = (Label)rw.FindControl("Label10");
        Label r = (Label)rw.FindControl("Label12");

        TextBox1.Text = name.Text;
        TextBox2.Text = desc.Text;
        TextBox3.Text = q.Text;
        TextBox4.Text = r.Text;
        Button1.Enabled = true;
        Button1.Text = "Update";
        i1 = rw.DataItemIndex;

    }
 
protected void Button1_Click(object sender, EventArgs e)
    {
        if (Button1.Text == "Update")
        {
            dt = (DataTable)Session["prd"];
            DataRow dr = dt.Rows[i1];
            dr["pname"] = TextBox1.Text;
            dr["pdesc"] = TextBox2.Text;
            dr["qty"] = TextBox3.Text;
            dr["rate"] = TextBox4.Text;
            Button1.Text = "Add";
         }
    }

 protected void grid()
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Session["prd"] = dt;

    }

0 comments:

Post a Comment