setting the content of a textbox in a GridView template field programatically

Posted on 2012-09-17
Last Modified: 2012-09-17
Excuse a newby's etiquette, I just posted this question as a comment in a previously answered question and am afraid that no one will see it and respond.

I have a DataGrid in which I set some of the columns as bound fields from a datasource.  There are several columns, not bound, that I want to either allow the user to enter, or to get the data from other sources and fill in arbitrarily.  It is no problem having the user enter the data and retrieving what they have entered.

In the example below, I have omitted all the fields except the one I am trying to manipulate.  What I want to do is update the contents of the cell in the DataGrid, not access the contents.

Here is my DataGrid:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
        AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" 
        <RowStyle BackColor="#E3EAEB" />
       <asp:TemplateField HeaderText="First Name">
                      <asp:TextBox ID="tbxFname" runat="server"></asp:TextBox>
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <AlternatingRowStyle BackColor="White" />

Open in new window

In the code behind I want to put text into the tbxFname textbox of the template field.  I cannot find the correct construct to do this.  I can easily get the contents of it, but actually modifying its contents is eluding me.

GridView1.Rows[index].Cells[1].Controls[0].Text = "some string";

Open in new window

 is wrong - .Text is not a valid property.  I have tried many other combinations of attributes with no success.  Any help?
Edit Comment
Question by:kparent
    LVL 18

    Expert Comment

    by:Jerry Miller
    You have to create a textbox in the code behind and assign it's value from the one in the gridview that you need to manipulate.

    The VB version is something like:

    dim txtBox as textbox = GridView1.Rows[index].Cells[1].Controls[0]

    Author Comment

    I've been able to do this without problem.  The problem is how to put the value back into the GridView cell.
    LVL 17

    Accepted Solution

    Try like this.
    Note :  FindControl method should take control id which you are looking for...

    TextBox LastName = (TextBox)GridView1.Rows[index].Cells[1].FindControl("tbxFname");
    LastName.Text = "Some string";

    Author Closing Comment

    Thanks.  It was not intuitive to me but it worked perfectly.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now