Please refer this code
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">
$(function () {
$("[id*=btnUpdate]").hide();
$("body").on("click", "[id*=btnAddRow]", function () {
var id = $("[id*=txtId]").val();
var name = $("[id*=txtName]").val();
var country = $("[id*=txtCountry]").val();
if (id != "" && name != "" && country != "") {
var row = $("[id*=GridView1] tr:last").clone();
$("td:nth-child(1)", row).html(id);
$("td:nth-child(2)", row).html(name);
$("td:nth-child(3)", row).html(country);
$("[id*=GridView1] tbody").append(row);
}
$("[id*=txtId]").val('');
$("[id*=txtName]").val('');
$("[id*=txtCountry]").val('');
return false;
});
$("body").on("click", "[id*=btnUpdate]", function () {
var rowIndex = $("[id*=hdEditedRow]").val();
var row = $("[id*=GridView1] tr:nth-child(" + rowIndex + ")");
$("td:nth-child(1)", row).html($("[id*=txtId]").val());
$("td:nth-child(2)", row).html($("[id*=txtName]").val());
$("td:nth-child(3)", row).html($("[id*=txtCountry]").val());
$("[id*=btnAddRow]").show();
$(this).hide();
$("[id*=txtId]").val('');
$("[id*=txtName]").val('');
$("[id*=txtCountry]").val('');
return false;
});
$("body").on("click", ".editRow", function () {
$("[id*=txtId]").val($.trim($(".Id", $(this).closest("tr")).html()));
$("[id*=txtName]").val($.trim($(".Name", $(this).closest("tr")).html()));
$("[id*=txtCountry]").val($.trim($(".Country", $(this).closest("tr")).html()));
$("[id*=hdEditedRow]").val($(this).parents('tr').index() + 1);
$("[id*=btnAddRow]").hide();
$("[id*=btnUpdate]").show();
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Id
</td>
<td>
<asp:TextBox ID="txtId" runat="server" />
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" />
</td>
</tr>
</table>
<br />
<asp:Button ID="btnAddRow" Text="Add Row" runat="server" />
<asp:Button ID="btnUpdate" Text="Update Row" runat="server" />
<br />
<br />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" ItemStyle-CssClass="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" ItemStyle-CssClass="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" ItemStyle-CssClass="Country" HeaderText="Country"
ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<a href="#" class="editRow">Select</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hdEditedRow" runat="server" />
</div>
</form>
</body>
</html>
Namspace
using System.Data;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
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");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">
$(function () {
$("[id*=btnUpdate]").hide();
$("body").on("click", "[id*=btnAddRow]", function () {
var id = $("[id*=txtId]").val();
var name = $("[id*=txtName]").val();
var country = $("[id*=txtCountry]").val();
if (id != "" && name != "" && country != "") {
var row = $("[id*=GridView1] tr:last").clone();
$("td:nth-child(1)", row).html(id);
$("td:nth-child(2)", row).html(name);
$("td:nth-child(3)", row).html(country);
$("[id*=GridView1] tbody").append(row);
}
$("[id*=txtId]").val('');
$("[id*=txtName]").val('');
$("[id*=txtCountry]").val('');
return false;
});
$("body").on("click", "[id*=btnUpdate]", function () {
var rowIndex = $("[id*=hdEditedRow]").val();
var row = $("[id*=GridView1] tr:nth-child(" + rowIndex + ")");
$("td:nth-child(1)", row).html($("[id*=txtId]").val());
$("td:nth-child(2)", row).html($("[id*=txtName]").val());
$("td:nth-child(3)", row).html($("[id*=txtCountry]").val());
$("[id*=btnAddRow]").show();
$(this).hide();
$("[id*=txtId]").val('');
$("[id*=txtName]").val('');
$("[id*=txtCountry]").val('');
return false;
});
$("body").on("click", ".editRow", function () {
$("[id*=txtId]").val($.trim($(".Id", $(this).closest("tr")).html()));
$("[id*=txtName]").val($.trim($(".Name", $(this).closest("tr")).html()));
$("[id*=txtCountry]").val($.trim($(".Country", $(this).closest("tr")).html()));
$("[id*=hdEditedRow]").val($(this).parents('tr').index() + 1);
$("[id*=btnAddRow]").hide();
$("[id*=btnUpdate]").show();
return false;
});
});
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Id
</td>
<td>
<input name="txtId" type="text" id="txtId" />
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<input name="txtName" type="text" id="txtName" />
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<input name="txtCountry" type="text" id="txtCountry" />
</td>
</tr>
</table>
<br />
<input type="submit" name="btnAddRow" value="Add Row" id="btnAddRow" />
<input type="submit" name="btnUpdate" value="Update Row" id="btnUpdate" />
<br />
<br />
<div>
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse: collapse;">
<tr style="color: White; background-color: #3AC0F2;">
<th scope="col">
Id
</th>
<th scope="col">
Name
</th>
<th scope="col">
Country
</th>
<th scope="col">
</th>
</tr>
<tr>
<td class="Id" style="width: 30px;">
1
</td>
<td class="Name" style="width: 150px;">
John Hammond
</td>
<td class="Country" style="width: 150px;">
United States
</td>
<td>
<a href="#" class="editRow">Select</a>
</td>
</tr>
<tr>
<td class="Id" style="width: 30px;">
2
</td>
<td class="Name" style="width: 150px;">
Mudassar Khan
</td>
<td class="Country" style="width: 150px;">
India
</td>
<td>
<a href="#" class="editRow">Select</a>
</td>
</tr>
<tr>
<td class="Id" style="width: 30px;">
3
</td>
<td class="Name" style="width: 150px;">
Suzanne Mathews
</td>
<td class="Country" style="width: 150px;">
France
</td>
<td>
<a href="#" class="editRow">Select</a>
</td>
</tr>
<tr>
<td class="Id" style="width: 30px;">
4
</td>
<td class="Name" style="width: 150px;">
Robert Schidner
</td>
<td class="Country" style="width: 150px;">
Russia
</td>
<td>
<a href="#" class="editRow">Select</a>
</td>
</tr>
</table>
</div>
<input type="hidden" name="hdEditedRow" id="hdEditedRow" />
</body>
</html>
Demo