Hi Vincenzo67,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
OnRowEditing="gvCustomers_RowEditing" OnRowDataBound="gvCustomers_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="true" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry1" runat="server" TextMode="SingleLine"
Text='<%# Eval("Country") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCountry2" runat="server" TextMode="SingleLine"
Text='<%# Eval("Country") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
VB
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id"), new DataColumn("Country") });
dt.Rows.Add(1, "India");
dt.Rows.Add(2, "USA");
dt.Rows.Add(3, "Nepal");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
protected void OnUpdate(object sender, EventArgs e)
{
}
protected void OnCancel(object sender, EventArgs e)
{
}
protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
{
TextBox tb = e.Row.FindControl("txtCountry2") as TextBox;
tb.Text = string.Empty;
tb.Enabled = false;
}
}
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id"), New DataColumn("Country")})
dt.Rows.Add(1, "India")
dt.Rows.Add(2, "USA")
dt.Rows.Add(3, "Nepal")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub gvCustomers_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gvCustomers.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Protected Sub gvCustomers_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowState = DataControlRowState.Edit Then
Dim tb As TextBox = TryCast(e.Row.FindControl("txtCountry2"), TextBox)
tb.Text = String.Empty
tb.Enabled = False
End If
End Sub
Screenshot