Reference:
http://aspsnippets.com/Articles/Display-GridView-Selected-Row-data-in-TextBox-outside-GridView-in-ASPNet.aspx
HTML:
<form id="form1" runat="server">
<div>
Enter OrderId
<asp:TextBox ID="txtOrderId" runat="server"> </asp:TextBox>
<asp:Button ID="btnSearch" Text="Search" runat="server" OnClick="btnSearch_Click" />
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="Gridview1_OnSelectedIndexChanged"
AutoGenerateSelectButton="true">
<Columns>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" Text='<%# Eval("LastName")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" Text='<%# Eval("Country")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
LastName:
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
Country:
<asp:TextBox ID="txtCountry" runat="server" />
</div>
</form>
VB:
Protected Sub Gridview1_OnSelectedIndexChanged(sender As Object, e As EventArgs)
Dim row As GridViewRow = Gridview1.SelectedRow
Me.txtLastName.Text = TryCast(row.FindControl("lblLastName"), Label).Text
Me.txtCountry.Text = TryCast(row.FindControl("lblCountry"), Label).Text
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Dim sqlStatment As String = "SELECT Distinct LastName,Country FROM Employees employees INNER JOIN Orders orders ON orders.EmployeeID = employees.EmployeeID WHERE OrderId = @OrderId"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(sqlStatment, con)
Using da As New SqlDataAdapter(cmd)
cmd.Parameters.AddWithValue("@OrderId", Me.txtOrderId.Text.Trim())
Dim ds As New DataSet()
da.Fill(ds)
Me.Gridview1.DataSource = ds
Me.Gridview1.DataBind()
End Using
End Using
End Using
End Sub
Using Row Command:
<form id="form1" runat="server">
<div>
Enter Country
<asp:TextBox ID="txtSearchEmployeeByCountry" runat="server"> </asp:TextBox>
<asp:Button ID="btnSearch" Text="Search" runat="server" OnClick="btnSearch_Click" />
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowCommand="Gridview1_OnRowCommand">
<Columns>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" Text='<%# Eval("LastName")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" Text='<%# Eval("Country")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandArgument='<%# Eval("LastName") %>'
CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
LastName:
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
Country:
<asp:TextBox ID="txtCountry" runat="server" />
</div>
</form>
VB:
Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Dim sqlStatment As String = "SELECT LastName, Country FROM Employees WHERE Country = @Country"
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(sqlStatment, con)
Using da As New SqlDataAdapter(cmd)
cmd.Parameters.AddWithValue("@Country", Me.txtSearchEmployeeByCountry.Text.Trim())
Dim ds As New DataSet()
da.Fill(ds)
Me.Gridview1.DataSource = ds
Me.Gridview1.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub Gridview1_OnRowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName.ToUpper() = "SELECT" Then
Dim gvr As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim RowIndex As Integer = gvr.RowIndex
Me.txtLastName.Text = TryCast(gvr.FindControl("lblLastName"), Label).Text
Me.txtCountry.Text = TryCast(gvr.FindControl("lblCountry"), Label).Text
End If
End Sub
Thank You.