Friday, August 17, 2012

How to get / edit values from grid view

Html code for grid view 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Grid View Edit</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GvStudentsList" runat="server" AutoGenerateColumns="false" Width="45%"
            HeaderStyle-ForeColor="White" HeaderStyle-Height="50">
            <Columns>
                <asp:TemplateField HeaderStyle-BackColor="ActiveCaption" HeaderText="Name" ItemStyle-HorizontalAlign="Center"
                    ItemStyle-Height="40">
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-BackColor="ActiveCaption" HeaderText="Quantity" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Label ID="lblquantity" runat="server" Text='<%# Eval("Quantity")  %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-BackColor="ActiveCaption" HeaderText="Price" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Label ID="lblprice" runat="server" Text='<%# Eval("Price")  %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderStyle-BackColor="ActiveCaption" HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgeditbtn" runat="server" ImageUrl="~/Images/Edit.gif" OnClick="ImgbtnEdit_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Add the text box to display values
     <br />
        <asp:Panel ID="Panel1" runat="server" Width="600px" Height="60px">
            <asp:Label ID="lbl1" runat="server" Text="Name"></asp:Label>
            <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            <asp:Label ID="lbl2" runat="server" Text="Quantity"></asp:Label>
            <asp:TextBox ID="txtquantity" runat="server"></asp:TextBox>
            <asp:Label ID="lbl3" runat="server" Text="Price"></asp:Label>
            <asp:TextBox ID="txtgrade" runat="server"></asp:TextBox>
        </asp:Panel>
    </div>
    </form>
</body>
</html>


  cs code for button click 

 protected void ImgbtnEdit_Click(object sender, EventArgs e)
    {
        ImageButton imgbtnEdit = (ImageButton)sender;
        GridViewRow GVRow = (GridViewRow)imgbtnEdit.NamingContainer;
        Label lblname = (Label)GVRow.FindControl("lblname");
        Label lblQuantity = (Label)GVRow.FindControl("lblquantity");
        Label lblPrice = (Label)GVRow.FindControl("lblprice");


        txtname.Text = lblname.Text;
        txtquantity.Text = lblQuantity.Text;
        txtgrade.Text = lblPrice.Text;      
        
    }

Click the Edit Button to get the values in textbox



No comments:

Post a Comment