Follow the Steps below...
1. First Fill the Grid of the page MainDataPage.aspx, In this Grid Pass the Id Of MainDataTable with the Navigate Url.
2. Make a Stored Proc to Select the Query that you have already stored in DetailedDataTable
that may look like
CREATE PROC [dbo].[SP_SelectQuery]
@Id INT
AS
BEGIN
DECLARE @eQuery NVARCHAR(MAX)
SET @eQuery=(SELECT Query FROM dbo.DetailedDataTable DDT INNER JOIN dbo.MainDataTable MDT ON
DDT.QueryId=MDT.QueryId WHERE MDT.Id=@Id)
EXEC(@eQuery)
END
GO
3. In DetailedDataPage.aspx take another grid and fill that grid with the above written stored proc by passing the Id value that you have given in Navigate Url of First MainDataPage.aspx.
Hope this would help you.