Solved

Dynamic Gridview hyperlink problem

Posted on 2014-11-06
3
188 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

895 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

14 Experts available now in Live!

Get 1:1 Help Now