Hey Warren,
Please refer below sample.
HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function () {
setTimeout(function () {
$('#gvtfaDetails').focus();
document.activeElement.scrollIntoView(true);
}, 1);
});
</script>
<div>
<asp:GridView ID="gvtfaTransaction" runat="server" AutoGenerateColumns="False" AllowSorting="True"
OnSorting="OnSorting" OnRowDataBound="gvtfaTransaction_RowDataBound" OnPageIndexChanging="OnPageIndexChanging"
CssClass="table table-striped table-bordered table-hover" DataKeyNames="CustomerId"
Width="98%" HorizontalAlign="Center" OnSelectedIndexChanged="gvtfaTransaction_SelectedIndexChanged1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Eval("ContactName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonSelect" runat="server" Text="Select" CommandName="Select"
CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="lightyellow" />
</asp:GridView>
<asp:GridView ID="gvtfaDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerId"
ShowFooter="True" AllowPaging="True" AllowSorting="False" OnRowDataBound="gvtfaDetails_RowDataBound"
OnSorting="OnSorting" CssClass="table table-striped table-bordered table-hover"
Width="98%" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ShowHeader="false"
HeaderStyle-Wrap="false" SortExpression="CustomerId" />
<asp:TemplateField HeaderText="4. Check Memo" SortExpression="CompanyName">
<ItemTemplate>
<asp:Label ID="LabelCheckMemo" runat="server" Text='<%# Eval("CompanyName") %>' Visible="false"></asp:Label>
<asp:TextBox ID="TextCheckMemo" runat="server" Text='<%# Eval("City") %>' Visible="false"
AutoPostBack="true" MaxLength="30"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" ShowHeader="false"
HeaderStyle-Wrap="false" SortExpression="PostalCode" />
</Columns>
</asp:GridView>
</div>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string strconstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strconstr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
gvtfaTransaction.DataSource = dt;
gvtfaTransaction.DataBind();
}
}
}
}
}
}
protected void gvtfaTransaction_SelectedIndexChanged1(object sender, EventArgs e)
{
int rowIndex = Convert.ToInt32(gvtfaTransaction.SelectedRow.RowIndex);
GridViewRow row = gvtfaTransaction.Rows[rowIndex];
string name = (row.FindControl("lblName") as Label).Text;
string strconstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strconstr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE ContactName=@Name", con))
{
cmd.Parameters.AddWithValue("@Name", name);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dtNew = new DataTable())
{
da.Fill(dtNew);
gvtfaDetails.DataSource = dtNew;
gvtfaDetails.DataBind();
}
}
}
}
}
protected void gvtfaDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void OnSorting(object sender, GridViewSortEventArgs e)
{
}
protected void gvtfaTransaction_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim strconstr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(strconstr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
gvtfaTransaction.DataSource = dt
gvtfaTransaction.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Protected Sub gvtfaTransaction_SelectedIndexChanged1(sender As Object, e As System.EventArgs) Handles gvtfaTransaction.SelectedIndexChanged
Dim rowIndex As Integer = Convert.ToInt32(gvtfaTransaction.SelectedRow.RowIndex)
Dim row As GridViewRow = gvtfaTransaction.Rows(rowIndex)
Dim name As String = (TryCast(row.FindControl("lblName"), Label)).Text
Dim strconstr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(strconstr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers WHERE ContactName=@Name", con)
cmd.Parameters.AddWithValue("@Name", name)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dtNew As DataTable = New DataTable()
da.Fill(dtNew)
gvtfaDetails.DataSource = dtNew
gvtfaDetails.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnPageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvtfaTransaction.PageIndexChanging
End Sub
Protected Sub gvtfaTransaction_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvtfaTransaction.RowDataBound
End Sub
Protected Sub gvtfaDetails_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvtfaDetails.RowDataBound
End Sub
Protected Sub OnSorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvtfaTransaction.Sorting
End Sub
Screenshot