Ok, so the "real" issue is we need to click on a row - but for now, lets just drop in a button to the GV, and use that.
Also, as a general rule, you don't need (or want) to expose PK id values in the grid. (it is a security issue, and those numbers tend to mean nothing to the user.
Thankfully, the elfs and wizards in Redmond cooked up a feature for this purpose (Datakeys of the GV).
So, in the GV, we can drop in a plane jane asp.net button. Say like this:
And our code to load, looks like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Using conn As New SqlConnection(My.Settings.TEST4)
Dim strSQL As String =
"SELECT ID,Fighter, Engine, Thrust, Description, FirstFlight, ImagePath from Fighters"
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
Dim rstData As New DataTable
rstData.Load(cmdSQL.ExecuteReader)
GridView1.DataSource = rstData
GridView1.DataBind()
End Using
End Using
End Sub
Ok, so now we see this:
So, we dropped in that plane jane button, and that will become our row select, and the code looks like this:
Protected Sub cmdView_Click(sender As Object, e As EventArgs)
' get the row
Dim btn As Button = sender
Dim gRow As GridViewRow = btn.NamingContainer
Dim iRowIndex As Integer = gRow.RowIndex
Dim iPK As Integer = GridView1.DataKeys(iRowIndex).Item("ID")
' OK, we have both row index (don't think we need it)
' and we have database PK row ID.
'lets now jump to a new page to display more information about this row we
' clicked on
Session("RowID") = iPK
Response.Redirect("ShowDetails.aspx")
End Sub
So, once we have that "row id", we can quite much do what we want. So, on the next page, the code to load, get use of that full one database row could be:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' code here to load the ONE row.
Dim strSQL As String = "SELECT * from Fighers where id = " & Session("RowID")
Dim rstData As New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
End If
End Sub
Note that you might not need nor want the "whole" row, and you can certain in that row click event code, grab any collum value from gRow, say like this:
dim strFigher as string = gRow.Cells(0).Text
So, you can pass in session one value from the gRow, or as noted, pass the "pk" row id, and then in the next web page you jump to, pull the whole database row again in that next page.
Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada