Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display a Gridview data onto forms Text Box controls after clicking an edit button.
what should i do.
Edit button is in a Grid views item template column.
please give me simple solution.
Posted

If you want to let the user edit the values in the gridview once they click on some "Edit" button in the gridview row then you can use the editItemTemplate. You might refer a simple article to do so on codeproject at Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List[^]

Regards
Pawan
 
Share this answer
 
Displaying Gridviews data onto the forms Text box[^]


in this link you can get basic idea.
 
Share this answer
 
Comments
Sachin Shinde11088 18-Dec-12 0:46am    
hey VishwaKL i have read ur article.
bt its not working in my case yaar.
is der need to set any property.
please let me know
Try this...

XML
<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
             AutoGenerateEditButton="True" onrowcancelingedit="GridView2_RowCancelingEdit" onrowediting="GridView2_RowEditing"
             >
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNameGrid" runat="server" Text ='<%# Bind("Name") %> ' ></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %> '></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Age">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtAgeGrid" runat="server" Text = '<%# Bind("Age") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %> '></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
     </div>
     <div>
     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
     <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
     </div>
    </form>



C#
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int key { get; set; }

}


public List<Person> obj = new List<Person>();
protected void Page_Load(object sender, EventArgs e)
{
obj = new List<Person>{(new Person{ Name = "ajai", Age = 50 ,key =10})};
obj.Add(new Person { Name = "Manoj", Age = 100, key = 20 });
obj.Add(new Person { Name = "Sanjay", Age = 150, key = 30 });

GridView2.DataSource = obj;
GridView2.DataBind();


}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
GridView2.DataSource = obj;
GridView2.DataBind();
TextBox editName = (TextBox)GridView2.Rows[e.NewEditIndex].Cells[0].FindControl("txtNameGrid");
TextBox editAge = (TextBox)GridView2.Rows[e.NewEditIndex].Cells[1].FindControl("txtAgeGrid");
txtName.Text = editName.Text;
txtAge.Text = editAge.Text;
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
GridView2.DataSource = obj;
GridView2.DataBind();

}
 
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