Avatar of Clever_Bob
Clever_BobFlag for Australia

asked on 

Dynamic Gridview hyperlink problem

So I'm adding columns and rows dynamically to a DataTable but it just seems to be impossible to include a hyperlink in the data. Its like the <A> tag is explicitly rejected inside the gridview when it is presented.

I know the approved way to do this is to add hyperlink fields to the gridview control in the HTML but because this is built so dynamically (requires various columns/rows) - I don't think thats possible

Here is the sub....



    Private Sub MakeLeagueSummaryGrid(ByVal leaguename As String, ByVal totalTeams As Integer, ByVal totalScheduledGames As Integer)
        Dim totalmatches As Integer = totalTeams / 2
        Dim NoOfWeeks As Integer = 0
        Try
            NoOfWeeks = totalScheduledGames / totalmatches

        Catch ex As Exception
            NoOfWeeks = 0
        End Try


        Dim dt As New DataTable
        Dim dcRound As New DataColumn("Round", GetType(Integer))
        Dim dcDate As New DataColumn("Date Played", GetType(String))

       
        dt.Columns.Add(dcRound)
        dt.Columns.Add(dcDate)


       

        For i As Integer = 0 To totalmatches - 1
            dt.Columns.Add(New DataColumn("Game #" & (i + 1), GetType(String)))

        Next

        Dim drow As DataRow

        For weekcount = 0 To NoOfWeeks * 2 + 1
            drow = dt.NewRow
            If dt.Rows.Count >= weekcount - 2 Then
                drow.Item("Round") = weekcount + 1
            End If



            dt.Rows.Add(drow)

        Next

        Dim db As New SQLiteEntities

        Dim str As String = "select ZhTeamName ||  "";;;;"" || z.ZHDATE ||  "";;;;"" || ZhFinalScore ||  "";;;;"" || ZHTIMEELAPSED from ZHISTORYTEAM zh, Zhistory z   where zh.ZHISTORY = z.Z_PK and z.ZHSeason = (select  ZHSeason from Zhistory Order by ZHseason desc limit 1)  AND ZHLEAGUENAME ='" & leaguename & "' order by z.[ZHDATE] asc "
        Dim hst = db.ExecuteStoreQuery(Of String)(str).ToList()

        Dim rowNo As Integer = 0
        Dim teamResult As String = ""
        Dim rowNumber As Integer = -1
        Dim colNumber As Integer = 1
        Dim curDate As Date = "1/1/1900"
        Dim isFirstEmptyRow As Boolean = False
        'Dim grp = hst.GroupBy(Function(x) x.Id, Function(x) x.Name)
        For Each grp In hst



            Dim Group() As String = grp.ToString().Split(";;;;")
            If (rowNo Mod 2 = 0) Then

                If (Group(12) > 0) Then
                    teamResult = Group(0) & " " & Group(8)
                Else
                    teamResult = Group(0) & " "
                End If


                'drow = dt.NewRow
                'drow("Id") = Group(0)
                'drow("Team 1") = Group(4)
                'drow("Match Date") = UnixTimeStampToDateTime(Group(8))
                'drow("Score 1") = Group(12)
                'drow("Details") = Group(16)
                'Dim words() As String

                'words = drow("Name").ToString().Split(";;;;")
                'Dim dtDate As DateTime = UnixTimeStampToDateTime(words(4)).Date
                'drow("Name") = dtDate + " , " + words(0)


            Else
                If (Group(12) > 0) Then
                    teamResult = teamResult & " vs " & Group(0) & " " & Group(8)
                    Dim dtDate As Date = Date.Parse(UnixTimeStampToDateTime(Group(4)))

                    If curDate.ToShortDateString() <> dtDate.ToShortDateString() Then
                        curDate = dtDate.ToShortDateString()
                        rowNumber = rowNumber + 1
                    End If
                    colNumber = colNumber + 1
                    dt.Rows(rowNumber).BeginEdit()
                    Dim dateString = dtDate.ToShortDateString()
                    dt.Rows(rowNumber).Item(1) = dateString
                    ' dt.Rows(rowNumber).Item(colNumber) = "<<a href=""GameStatDetails.aspx?Id=185&amp;team1=Doomsday Bunnies&amp;team2=B.I.A.C""> " & teamResult & "</a>" 'this is where the socres are added and I want to add link to game

                    ' dt.Rows(rowNumber).Item(2).navigateURL =
                    'colNumber.NavigateUrl = "~\views\view_prospect.aspx?id=" & dt.Rows(column index).Item("ID").ToString

                    Dim testlink = "<a href='simple'>AAAA</a>"

                    dt.Rows(rowNumber).Item(colNumber) = teamResult & testlink



                    'dt.Rows(rowNumber).Item(colNumber) = "<A href='simple'>AAAA</>"

                    '  dt.Rows(rowNumber).Item(colNumber).navigateURL = "GameStatDetails.aspx?Id=185&amp;team1=Doomsday Bunnies&amp;team2=B.I.A.C"

                    dt.Rows(rowNumber).Item("Round") = rowNumber + 1
                    dt.Rows(rowNumber).EndEdit()
                    teamResult = ""
                    If colNumber = dt.Columns.Count - 1 Then
                        colNumber = 1
                    End If
                Else
                    If (isFirstEmptyRow = False) Then
                        rowNumber = rowNumber + 1
                        isFirstEmptyRow = True
                    End If
                    teamResult = teamResult & " vs " & Group(0)
                    Dim dtDate As Date = Date.Parse(UnixTimeStampToDateTime(Group(4)))




                    colNumber = colNumber + 1
                    dt.Rows(rowNumber).BeginEdit()
                    Dim dateString = dtDate.ToShortDateString()
                    dt.Rows(rowNumber).Item(1) = ""
                    dt.Rows(rowNumber).Item(colNumber) = teamResult
                    dt.Rows(rowNumber).Item("Round") = rowNumber + 1
                    dt.Rows(rowNumber).EndEdit()
                    teamResult = ""
                    If colNumber = dt.Columns.Count - 1 Then
                        colNumber = 1
                        rowNumber = rowNumber + 1
                    End If
                End If





                'If drow("Score 1") <> drow("Score 2") Then
                '    drow("Result") = drow("Team 1") + vbTab + drow("Score 1") + "  -  " + drow("Team 2") + "   " + drow("Score 2")
                'Else
                '    drow("Result") = "Mtach drawn with " + drow("Score 1") + " - " + drow("Score 2")
                'End If




            End If


            rowNo = rowNo + 1

        Next

        If dt.Rows.Count > 2 Then
            DeleteFromDataTable(dt, "[Game #1] IS NULL")
        End If


        Dim actualRoundnumber As Integer = 0
        For i As Integer = 0 To dt.Rows.Count - 1
            Try
                If dt.Rows(i).Item("Date Played") = "" Then
                    dt.Rows(i).BeginEdit()
                    dt.Rows(i).Item("Round") = dt.Rows.Count - i
                    dt.Rows(i).EndEdit()
                Else
                    actualRoundnumber = actualRoundnumber + 1
                    dt.Rows(i).BeginEdit()
                    dt.Rows(i).Item("Round") = actualRoundnumber
                    dt.Rows(i).EndEdit()
                End If
            Catch ex As Exception

            End Try


        Next



        dt.DefaultView.Sort = "[Round] ASC"





        grdLeagueSummary.DataSource = dt.DefaultView
        grdLeagueSummary.DataBind()
    End Sub
Microsoft SQL ServerMicrosoft DevelopmentVisual Basic.NET

Avatar of undefined
Last Comment
Vadim Rapp
Avatar of Vadim Rapp
Vadim Rapp
Flag of United States of America image

Don't put HTML  into the datatable, put only the URL itself.

Open Gridview's "Edit Columns" and add HyperLink field. Specify its properties DataNavigateURL as the field with the URL, and DataNavigateText.
Avatar of Clever_Bob
Clever_Bob
Flag of Australia image

ASKER

I don't think that can be achieved when you are dynamically building a databable in the code and then binding it to the gridview? How do you reference the   DataNavigateURL property?
ASKER CERTIFIED SOLUTION
Avatar of Vadim Rapp
Vadim Rapp
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo