Dear tanweeruddinb...,
Kindly check below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="ID" DataField="CustomerId" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Country" DataField="Country" />
<asp:TemplateField HeaderText="Modify">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="OnEdit" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" Visible="false" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
protected void OnEdit(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
int index = row.RowIndex;
for (int i = 0; i < gvCustomers.Rows.Count; i++)
{
if (index == i)
{
(gvCustomers.Rows[i].FindControl("btnUpdate") as Button).Visible = true;
(gvCustomers.Rows[i].FindControl("btnEdit") as Button).Visible = false;
}
else
{
(gvCustomers.Rows[i].FindControl("btnEdit") as Button).Visible = false;
}
}
}
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 conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnEdit(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow)
Dim index As Integer = row.RowIndex
For i As Integer = 0 To gvCustomers.Rows.Count - 1
If index = i Then
TryCast(gvCustomers.Rows(i).FindControl("btnUpdate"), Button).Visible = True
TryCast(gvCustomers.Rows(i).FindControl("btnEdit"), Button).Visible = False
Else
TryCast(gvCustomers.Rows(i).FindControl("btnEdit"), Button).Visible = False
End If
Next
End Sub
Screenshot