In this article I will explain how to bind data (values) to TextBox inside TemplateField column of GridView in ASP.Net using C# and VB.Net.
The TextBoxes will be populated with values from database using the DataBinder Eval function.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Bind data (values) to TextBox inside GridView in ASP.Net using C# and VB.Net
I have already inserted few records in the table.
Bind data (values) to TextBox inside GridView in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView consisting of TextBoxes inside TemplateField column of GridView.
The Text property of the TextBox will be dynamically populated from database using DataBinder Eval function.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Customer Id" ItemStyle-Width="30">
            <ItemTemplate>
                <asp:TextBox ID="txtCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Binding the GridView
Inside the Page Load event of the page, the GridView is populated with the records of the Customers Table.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers"))
            {
                cmd.Connection = con;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
                cmd.Connection = con
                Using sda As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub
 
 
Screenshot
The following screenshot displays the GridView consisting of TextBoxes whose values are populated from database.
Bind data (values) to TextBox inside GridView in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads