Ok, I like this question.
I am going to assume this:
We have a repeater - we pull the repeating rows from SQL server (lets say Hotels)
Then inside of the Repeater - we want to see the people booked in that hotel (lets say table People).
So, when we click in one of the repeating groups and inside that grid, we want BOTH the grid row, and then also the repeater row also, right?
Ok, so so lets assume this markup
---- Anyone here know how to paste markup on this page???
MY VERY sorry - the markup has to be a image. But it is this:
So not too bad. We have some information about the Hotel, and then we have that grid view inside showing the people booked for that repeater.
So, the above looks like this:
So our code to fill this out is thus this:
Dim MyTable As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
LoadGrid()
End If
End Sub
Sub LoadGrid()
Dim strSQL = "SELECT * FROM tblHotels WHERE Active = 1 ORDER BY HotelName"
Using cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.TEST4))
cmdSQL.Connection.Open()
MyTable.Load(cmdSQL.ExecuteReader)
Repeater1.DataSource = MyTable
Repeater1.DataBind()
End Using
End Sub
Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Select Case e.Item.ItemType.ToString
Case "Item", "AlternatingItem"
' create sql for grid - INCLUDE Repeater row in SQL!!!
' Get PK of this row - for child pull
Dim RepRow As Integer = e.Item.ItemIndex
Dim HotelPK As Integer = MyTable.Rows(RepRow).Item("ID") ' HotelPK
Dim strSQL As String =
"SELECT ID, FirstName, LastName, City," & e.Item.ItemIndex & " AS RepRow " &
" from People WHERE Hotel_ID = " & HotelPK & " Order by FirstName"
Dim MyGrid As GridView = e.Item.FindControl("GridView1")
Using cmdSQL As New SqlCommand(strSQL, New SqlConnection(My.Settings.TEST4))
cmdSQL.Connection.Open()
MyGrid.DataSource = cmdSQL.ExecuteReader
MyGrid.DataBind()
End Using
Case "Footer"
End Select
End Sub
Protected Sub btnSel_Click(sender As Object, e As EventArgs)
Dim btn As Button = sender
Dim intRepeatRow As Integer = btn.Attributes("MyRepeatRow")
Dim intGirdRow As Integer = btn.Attributes("MyGridRow")
' get repeater row
Dim RepeatData As RepeaterItem = Repeater1.Items(intRepeatRow)
Dim strHotel As String = TryCast(RepeatData.FindControl("txtHotelName"), Label).Text
' get grid from that repeater row
Dim myGrid As GridView = TryCast(RepeatData.FindControl("GridView1"), GridView)
Dim myGridRow As GridViewRow = myGrid.Rows(intGirdRow)
Dim strFirstNameFromGrid = TryCast(myGridRow.FindControl("txtFirst"), Label).Text
Debug.Print("Hotel Name (repeater) = " & strHotel)
Debug.Print("First Name from Grid = " & strFirstNameFromGrid)
End Sub
So, when we click on a row, the output is this:
Hotel Name (repeater) = Jasper Park Lodge
First Name from Grid = Scott
Note how by adding the extra attributes to the button - we don't even care about or deal with the repeater events - it just a button click - we get the two rows (repeat row, and grid row).
So look VERY VERY close how I added some custom attributes to the button markup in that screen cap.
Regards,
Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada