Hi SUJAYS,
Check this example. Now please take its reference and correct your code.
HTML
<div class="col-sm-12 p-0" runat="server" id="dvLocationDetails">
<div class="col-sm-6 p-0">
<div class="form-group col-sm-12 flex p-0">
<div class="col-sm-4 p-0">
<label class="pt-2">
Location <span class="text-danger">*</span>
</label>
</div>
<div class="col-sm-10 p-0">
<asp:TextBox ID="txtloc" runat="server" class="w-100 form-control"></asp:TextBox></div>
<div class="col-sm-10 p-0">
<asp:Button Text="Save" runat="server" OnClick="Save" /></div>
</div>
</div>
</div>
<div class="table-responsive">
<asp:GridView ID="gvLocation" runat="server" CellPadding="12" AutoGenerateColumns="False"
class="table table-striped" Width="100%" OnSelectedIndexChanged="gvLocation_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" OnClientClick="return confirm('Are you sure,you want to delete this record ?');"
Text="Delete" CommandArgument='<%# Eval("Id") %>' CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
dvLocationDetails.Visible = false;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvLocation.DataSource = dt;
gvLocation.DataBind();
}
}
protected void gvLocation_SelectedIndexChanged(object sender, EventArgs e)
{
dvLocationDetails.Visible = true;
txtloc.Text = gvLocation.SelectedRow.Cells[0].Text;
gvLocation.Visible = false;
}
protected void Save(object sender, EventArgs e)
{
gvLocation.Visible = true;
dvLocationDetails.Visible = false;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
dvLocationDetails.Visible = False
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gvLocation.DataSource = dt
gvLocation.DataBind()
End If
End Sub
Protected Sub gvLocation_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
dvLocationDetails.Visible = True
txtloc.Text = gvLocation.SelectedRow.Cells(0).Text
gvLocation.Visible = False
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
gvLocation.Visible = True
dvLocationDetails.Visible = False
End Sub
Screenshot