Gridview Hyperlink containing bound data values.

I am trying to use a gridview dynamically populated where the first column is a hyperlink field containing a tracking number from my database for each ticket.  I can't seem to get this to work?  Here's what I have so far in ASP and VB...

ASP:
              <asp:Panel ID="ExistingPanel" runat="server" Height="90px" 
                  ScrollBars="Vertical" Width="505px">
                  <asp:GridView ID="gvExisting" runat="server" Width="494px">     
                      <Columns>
                          <asp:HyperLinkField HeaderText="Tracking #  " />
                      </Columns>
                  </asp:GridView>
              </asp:Panel>

Open in new window


VB:
  Function LoadExisting() As ICollection
    Dim RecCnt As Integer = 0
    Dim connectionString As String = ConfigurationManager.ConnectionStrings("EDWCERTConnectionString").ConnectionString
    Dim connection As SqlConnection = New SqlConnection(connectionString)
    Dim SqlQry As StringBuilder = New StringBuilder
    Dim sql As String
    Dim dt As DataTable = New DataTable()
    Dim dr As DataRow

    dt.Columns.Add(New DataColumn("Tracking #", GetType(String)))
    dt.Columns.Add(New DataColumn("Description", GetType(String)))
    'dt.Columns.Add(New DataColumn("Submitter", GetType(String)))

    SqlQry.Clear()
    SqlQry.Append("SELECT * FROM AllRequests_Status_V WHERE es_contact_type_id = '1' ")

    Select Case rblExtStatus.SelectedIndex
      Case 0 ' All
      Case 1 ' Approved
        SqlQry.Append(" And es_review_status_values like '%Approv%' ")
      Case 2 ' In Review
        SqlQry.Append(" And Review_Status_ID = '4' ")
      Case 3 ' Rejected
        SqlQry.Append(" And Review_Status_ID = '6' ")
      Case 4 ' Withdrawn
        SqlQry.Append(" And Review_Status_ID = '7' ")
    End Select

    sql = SqlQry.ToString '"SELECT * FROM AllRequests_Status_V WHERE CERTIFY_IND = 'N' AND NT_USER_ID LIKE '" & Session("LoginId") & "%'"
    Dim command As SqlCommand = New SqlCommand(sql, connection)

    'lstbxExisting.Items.Clear()
    Try
      connection.Open()
      Dim reader As DbDataReader = command.ExecuteReader()

      Do While reader.Read()
        dr = dt.NewRow
        
        dr(0) = reader("Tracking_Number") 
        dr(0) = reader("Brief_Description_Purpose")
        dt.Rows.Add(dr)
        'lstbxExisting.Items.Add(reader("Tracking_Number").ToString)
        'RecCnt = RecCnt + 1
      Loop
    Catch ex As Exception
      Throw ex
    Finally
      connection.Close()
      If Not connection Is Nothing Then
        connection.Dispose()
      End If
      connection = Nothing
      command = Nothing
    End Try
    Dim dv As DataView = New DataView(dt)
    Return dv
  End Function

Open in new window

EDW_GideonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
By dynamic, do you mean variable number of columns? If not you can declare the grid in aspx code and populate it in code behind.
0
Lokesh B RDeveloperCommented:
Hi,

Add RowDataBound event and try

<asp:GridView ID="gvExisting" runat="server" Width="494px" OnRowDataBound="gvExisting_RowDataBound">
            </asp:GridView>

Open in new window


void gvExisting_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var linkColumn = e.Row.Cells[0];
        linkColumn.Controls.Clear();
        linkColumn.Controls.Add(new HyperLink { NavigateUrl = "http://www.google.com", Text = linkColumn.Text });
    }
}

Open in new window



Make Index correct here

dr(0) = reader("Tracking_Number") 
dr(1) = reader("Brief_Description_Purpose")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EDW_GideonAuthor Commented:
CodeCruser,

No same number of columns, I tried to configure the Grid and do it in code behind but every time I did that the columns added by the code behind were added to the right of the configured columns not in them?

Lokesh,

I will try your solution shortly, thanks.

(This is going to lead into a second question I will post.  I need the click event to trigger a code behind event not direct to a new address as shown in Lokesh's example.  Perhaps I'm approaching this wrong but I couldn't get the Gridview selected index event to fire so trying to add a link thinking that would work?)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.