Hi makumbi,
Please refer below sample.
HTML
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 { width: 68%; border: 1px solid #FF0000; height: 201px; }
.auto-style2 { height: 23px; }
.auto-style3 { border-style: groove; border-width: 3px; height: 23px; }
</style>
<script type="text/javascript" src="../Scripts/JqueryNumberFormatercomma.js"></script>
<script type="text/javascript" src="../Scripts/Jquery-3.5.1.js"></script>
<script type="text/javascript" src="../Scripts/Jquery.datatables.min.js"></script>
<script type="text/javascript" src="../Scripts/datatable.fixedColumns.min.js"></script>
<link href="../Scripts/Jquery.datatables.min.css" rel="stylesheet" type="text/css" />
<link href="../Scripts/fixedColumns.dataTables.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$('#StudentGrid0 tfoot tr').appendTo('#StudentGrid0 thead');
$('#StudentGrid0').removeAttr('width').DataTable({
bLengthChange: true,
lengthMenu: [[5, 10, -1], [5, 10, "All"]],
bFilter: true,
bSort: true,
paging: true,
fixedColumns: false,
orderCellsTop: true
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="auto-style1">
<tr>
<td class="auto-style3">
<asp:TextBox ID="singlek" runat="server"></asp:TextBox>
</td>
<td class="auto-style3">
<asp:TextBox ID="doubles" runat="server"></asp:TextBox>
</td>
<td class="auto-style3">
<asp:TextBox ID="route0" runat="server" Width="344px"></asp:TextBox>
</td>
<td class="auto-style3">
<asp:Label ID="EntryCode" runat="server"></asp:Label>
</td>
<td class="auto-style3" colspan="2">
<asp:Button ID="Button2" runat="server" Text="Update Data" />
</td>
</tr>
<tr>
<td class="auto-style2" colspan="6">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False" Height="16px"
ShowFooter="True" Width="532px" OnRowCommand="StudentGrid0_RowCommand">
<Columns>
<asp:TemplateField HeaderText="CustomerId">
<EditItemTemplate>
<asp:TextBox ID="txtCustomerId" runat="server" Text='<%# Bind("CustomerId") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftCustomerId" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Bind("CustomerId") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<EditItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftCountry" runat="server" Width="344px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="atk" HeaderText="EntryCode" />--%>
<asp:ButtonField CommandName="Edit1" Text="Select" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="5">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</td>
<td>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
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#
private string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
Bindroute();
}
private void Bindroute()
{
SqlCommand cmdt = new SqlCommand("SELECT * FROM Customers");
gvCustomers.DataSource = this.ExecuteQuery(cmdt, "SELECT");
gvCustomers.DataBind();
gvCustomers.UseAccessibleHeader = true;
gvCustomers.FooterRow.TableSection = TableRowSection.TableFooter;
gvCustomers.HeaderRow.TableSection = TableRowSection.TableHeader;
}
protected void StudentGrid0_RowCommand(object sender, GridViewCommandEventArgs e)
{
TextBox txtCustomerId = (gvCustomers.FooterRow.FindControl("ftCustomerId") as TextBox);
TextBox txtName = (gvCustomers.FooterRow.FindControl("ftName") as TextBox);
TextBox txtCountry = (gvCustomers.FooterRow.FindControl("ftCountry") as TextBox);
txtCustomerId.Text = string.Empty;
txtName.Text = string.Empty;
txtCountry.Text = string.Empty;
if (e.CommandName == "Edit1")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvCustomers.Rows[index];
Label lblCustomerId = (Label)row.FindControl("lblCustomerId");
Label lblName = (Label)row.FindControl("lblName");
Label lblCountry = (Label)row.FindControl("lblCountry");
txtCustomerId.Text = lblCustomerId.Text;
txtName.Text = lblName.Text;
txtCountry.Text = lblCountry.Text;
}
}
private DataTable ExecuteQuery(SqlCommand cmd, string action)
{
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
switch (action)
{
case "SELECT":
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
case "UPDATE":
{
con.Open();
cmd.ExecuteReader();
con.Close();
break;
}
}
return null;
}
}
VB.Net
Private conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then Bindroute()
End Sub
Private Sub Bindroute()
Dim cmdt As SqlCommand = New SqlCommand("SELECT * FROM Customers")
gvCustomers.DataSource = Me.ExecuteQuery(cmdt, "SELECT")
gvCustomers.DataBind()
gvCustomers.UseAccessibleHeader = True
gvCustomers.FooterRow.TableSection = TableRowSection.TableFooter
gvCustomers.HeaderRow.TableSection = TableRowSection.TableHeader
End Sub
Protected Sub StudentGrid0_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
Dim txtCustomerId As TextBox = (TryCast(gvCustomers.FooterRow.FindControl("ftCustomerId"), TextBox))
Dim txtName As TextBox = (TryCast(gvCustomers.FooterRow.FindControl("ftName"), TextBox))
Dim txtCountry As TextBox = (TryCast(gvCustomers.FooterRow.FindControl("ftCountry"), TextBox))
txtCustomerId.Text = String.Empty
txtName.Text = String.Empty
txtCountry.Text = String.Empty
If e.CommandName = "Edit1" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gvCustomers.Rows(index)
Dim lblCustomerId As Label = CType(row.FindControl("lblCustomerId"), Label)
Dim lblName As Label = CType(row.FindControl("lblName"), Label)
Dim lblCountry As Label = CType(row.FindControl("lblCountry"), Label)
txtCustomerId.Text = lblCustomerId.Text
txtName.Text = lblName.Text
txtCountry.Text = lblCountry.Text
End If
End Sub
Private Function ExecuteQuery(ByVal cmd As SqlCommand, ByVal action As String) As DataTable
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
Select Case action
Case "SELECT"
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
Case "UPDATE"
con.Open()
cmd.ExecuteReader()
con.Close()
Exit Select
End Select
Return Nothing
End Using
End Function