In this article I will explain with an example, how to make GridView TextBox ReadOnly (Disabled) in ASP.Net when Editing using C# and VB.Net.
The TextBox in ASP.Net GridView will be made ReadOnly or Disabled during the Edit Mode of GridView inside the Row being currently edited using C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Make GridView TextBox ReadOnly (Disabled) in ASP.Net
 
I have already inserted few records in the table.
Make GridView TextBox ReadOnly (Disabled) in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The below HTML Markup consists of a GridView control with AutoGenerateColumns property set to false.
The GridView consists of three BoundField columns and a CommandField column.
The GridView is specified with the OnRowEditing, OnRowCancelingEdit and OnRowUpdating events.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
    OnRowUpdating="OnRowUpdating">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" ItemStyle-Width="80" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
        <asp:CommandField ShowEditButton="true" ShowCancelButton="true" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 
Populating the GridView control
The GridView is populated using the records from the Customers table inside the Page Load event of the ASP.Net page.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (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
        Me.BindGrid()
    End If
End Sub
 
Private Sub BindGrid()
    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")
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub
 
 
Editing and Updating GridView records
Edit
When the Edit Button is clicked, the GridView’s OnRowEditing event handler is triggered. Here simply the EditIndex of the GridView is updated with the Row Index of the GridView Row to be edited and the GridView is populated with data.
After the GridView is populated, the TextBox of the First column i.e. CustomerId TextBox is referenced and made ReadOnly by setting the ReadOnly property to True or Disabled by setting the Enabled property to False.
C#
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    this.BindGrid();
 
    //Set the TextBox as ReadOnly.
    (GridView1.Rows[e.NewEditIndex].Cells[0].Controls[0] as TextBox).ReadOnly = true;
 
    //Set the TextBox as Disabled.
    (GridView1.Rows[e.NewEditIndex].Cells[0].Controls[0] as TextBox).Enabled = false;
}
 
VB.Net
Protected Sub OnRowEditing(sender As Object, e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    Me.BindGrid()
 
    'Set the TextBox as ReadOnly.
    TryCast(GridView1.Rows(e.NewEditIndex).Cells(0).Controls(0), TextBox).ReadOnly = True
 
    'Set the TextBox as Disabled.
    TryCast(GridView1.Rows(e.NewEditIndex).Cells(0).Controls(0), TextBox).Enabled = False
End Sub
 
Update
When the Update Button is clicked, the GridView’s OnRowUpdating event handler is triggered.
CustomerId which is the primary key is fetched from the TextBox inside the first Cell of the GridView while the Name and Country fields are fetched from their respective columns by referencing the TextBoxes in it.
Finally, an Update query is executed over the database table.
C#
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.RowIndex];
    int customerId = int.Parse((row.Cells[0].Controls[0] as TextBox).Text);
    string name = (row.Cells[1].Controls[0] as TextBox).Text;
    string country = (row.Cells[2].Controls[0] as TextBox).Text;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId"))
        {
            cmd.Parameters.AddWithValue("@CustomerId", customerId);
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Country", country);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    GridView1.EditIndex = -1;
    this.BindGrid();
}
 
VB.Net
Protected Sub OnRowUpdating(sender As Object, e As GridViewUpdateEventArgs)
    Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
    Dim customerId As Integer = Integer.Parse((TryCast(row.Cells(0).Controls(0), TextBox)).Text)
    Dim name As String = TryCast(row.Cells(1).Controls(0), TextBox).Text
    Dim country As String = TryCast(row.Cells(2).Controls(0), TextBox).Text
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId")
            cmd.Parameters.AddWithValue("@CustomerId", customerId)
            cmd.Parameters.AddWithValue("@Name", name)
            cmd.Parameters.AddWithValue("@Country", country)
            cmd.Connection = con
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Using
    End Using
    GridView1.EditIndex = -1
    Me.BindGrid()
End Sub
 
Cancel Edit
When the Cancel Button is clicked, the GridView’s OnRowCancelingEdit event handler is triggered. Here the EditIndex is set to -1 and the GridView is populated with data.
C#
protected void OnRowCancelingEdit(object sender, EventArgs e)
{
    GridView1.EditIndex = -1;
    this.BindGrid();
}
 
VB.Net
Protected Sub OnRowCancelingEdit(sender As Object, e As EventArgs)
    GridView1.EditIndex = -1
    Me.BindGrid()
End Sub
 
 
Screenshots
The GridView
Make GridView TextBox ReadOnly (Disabled) in ASP.Net
 
The GridView with ReadOnly CustomerId TextBox
Make GridView TextBox ReadOnly (Disabled) in ASP.Net
 
The GridView with Disabled CustomerId TextBox
Make GridView TextBox ReadOnly (Disabled) in ASP.Net
 
 
Demo
 
 
Downloads