Hii georgeacuster...,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
OnRowEditing="gvCustomers_RowEditing" OnDataBound="gvCustomers_DataBound"
OnRowCommand="gvCustomers_RowCommand">
<Columns>
<asp:BoundField DataField="tId" HeaderText="TId" ReadOnly="true" />
<asp:TemplateField HeaderText="TCity">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server"
Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCity" runat="server" TextMode="SingleLine"
Text='<%# Eval("City") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex %>' />
</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.Net
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("TId"), new DataColumn("City") });
dt.Rows.Add(1, "Mumbai");
dt.Rows.Add(2, "San Diago");
dt.Rows.Add(3, "Nice");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
protected void OnUpdate(object sender, EventArgs e)
{
gvCustomers.EditIndex = -1;
this.BindGrid();
}
protected void OnCancel(object sender, EventArgs e)
{
gvCustomers.EditIndex = -1;
this.BindGrid();
}
protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
this.BindGrid();
}
private bool IsEditRow = false;
private int CurrentEditRowindex = -1;
protected void gvCustomers_DataBound(object sender, EventArgs e)
{
if (IsEditRow && CurrentEditRowindex > -1)
{
for (int i = 0; i < gvCustomers.Rows.Count; i++)
{
if (i != CurrentEditRowindex)
{
gvCustomers.Rows[i].Visible = false;
}
}
}
}
protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
IsEditRow = true;
CurrentEditRowindex = Convert.ToInt32(e.CommandArgument);
}
else
{
IsEditRow = false;
CurrentEditRowindex = -1;
}
}
VB.Net
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("TId"), New DataColumn("City")})
dt.Rows.Add(1, "Mumbai")
dt.Rows.Add(2, "San Diago")
dt.Rows.Add(3, "Nice")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
gvCustomers.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
gvCustomers.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub gvCustomers_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
gvCustomers.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Private IsEditRow As Boolean = False
Private CurrentEditRowindex As Integer = -1
Protected Sub gvCustomers_DataBound(ByVal sender As Object, ByVal e As EventArgs)
If IsEditRow AndAlso CurrentEditRowindex > -1 Then
For i As Integer = 0 To gvCustomers.Rows.Count - 1
If i <> CurrentEditRowindex Then
gvCustomers.Rows(i).Visible = False
End If
Next
End If
End Sub
Protected Sub gvCustomers_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Edit" Then
IsEditRow = True
CurrentEditRowindex = Convert.ToInt32(e.CommandArgument)
Else
IsEditRow = False
CurrentEditRowindex = -1
End If
End Sub
Screenshot