Solved

Dynamic Gridview hyperlink problem

Posted on 2014-11-06
3
184 Views
Last Modified: 2015-01-22
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
0
Comment
Question by:Clever_Bob
  • 2
3 Comments
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40428215
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.
0
 
LVL 7

Author Comment

by:Clever_Bob
ID: 40429463
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?
0
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 500 total points
ID: 40429907
Anything in visual studio that can be achieved in designers, can also be achieved in code.

> How do you reference the   DataNavigateURL property?

one way:

    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) _
           Handles GridView1.DataBound
        Dim o As HyperLinkField = sender.Columns(2)
        o.DataNavigateUrlFields = New String() {"urfield"}
        o.DataTextField = "textfield"
    End Sub

This will show in each row the hyperlink with URL from urlfield and text from textfield, where urlfield and textfield are names of the fields in the datatable. You can make it even more convenient by using property DataNavigateURLFormatString, so, for example, the base URL comes from one column, then URL parameter from another.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now