Click here to Skip to main content
16,017,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Need someone to show how to get value from gridview column into textbox.
For example if I have "GridViewEmployee" with 4 columns ID, NAME,ADDRESS and EMAIL.
AND ALSO A TEMPLATEFIELD, IN TEMPLATEFIELD ONE LINKBUTTON IS PLACES INSIDE ITEMTEMPLATE. WHEN CLICK ON LINK BUTTON PARTICULAR VALUE FROM GRIDVIEW WILL DISPLAY ON ALL CORRESPONDING TEXTBOXES.

I'm still learning asp.net using C#, and I was trying to do many options, but somehow could not get result.

MY HTML CODE IS
ASP.NET
<pre lang="xml"><asp:GridView ID="empGV" runat="server" AutoGenerateColumns="False"
                        CellPadding="4" DataKeyNames="eid" DataMember="employee" ForeColor="#333333"
                        GridLines="None" Width="500px">
                        <RowStyle BackColor="#EFF3FB" />
                        <Columns>
                            <asp:BoundField DataField="eid" HeaderText="Employee Id" />
                            <asp:BoundField DataField="ename" HeaderText="Employee Name" />
                            <asp:BoundField DataField="eaddress" HeaderText="Employee Address" />
                            <asp:TemplateField HeaderText="Select">
                                <ItemTemplate>
                                <asp:LinkButton ID="lbSelect" runat="server">Edit</asp:LinkButton>
                                    <%--<asp:LinkButton ID="lbSelect" runat="server"
                                        CommandName="EDIT">Edit</asp:LinkButton>--%>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#2461BF" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>



AND CODEBEHIND CODE IS:

This is nearest I was with the code:
textbox1.text = GridViewEmployee.rows[0].cells(ID);
textbox2.text = GridViewEmployee.rows[0].cells(NAME);
textbox3.text = GridViewEmployee.rows[0].cells(ADDRESS);
textbox4.text = GridViewEmployee.rows[0].cells(EMAIL);
or i have also tried this one but both are not working
WHICH CODE I WILL USE "SELECTEDINDEX OR "ITEMCOMMAND" OR ANYTHING ELSE"
SOME TRIAL CODE IS HERE;
protected void empGV_SelectedIndexChanged(object sender, EventArgs e)
{

GridViewRow row = empGV.SelectedRow;
textbox1.Text = row.Cells[1].Text;
textbox2.Text = row.Cells[2].Text;


IF ANY HAVE CODE ABOUT GRIDVIEW MANIPULATION USING TEMPLATEFIELD NOT IN BUILT REASYMADE EDIT OR UPDATE TEMPLATE(COMMANDFIELD).

}


Thank you in advance for help to anyone.
Posted

1 solution

Hi Rajiv

try this

use ButtonField in place of TemplateField

<asp:buttonfield text="Edit" buttontype="Link" commandname="Delete" xmlns:asp="#unknown">


protected void empGV_RowCommand(object sender, GridViewDeleteEventArgs e)
{
/// Use // e.RowIndex to get value from grid
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900