• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1624
  • Last Modified:

Dynamically add a hyperlink column to the front of a dynamically created datagrid

Hello there!
I've got a datagrid I've dynamically created from a sproc.  Note I autogenerate the columns from a dataset (this may be a bad thing).  I want to either:
1) Add a hyperlink column in front of all other columns. The data in the 1st column of the datagrid will form part of the url for the new hyperlink column.  (Am I making myself clear???)

-- or --

2) Change the first column of the datagrid to be a hyperlink column using it's own data as the url.

So in either case the new hyperlink column will have a url that reads (as an example): job.aspx?id=343  (343 coming from the 1st column of the dataset).
I've tried a few techniques but I'm stuck.  I've tried adding a column and then using the grid's itemdatabound event.
Existing code is below, I've left in some of my awful attempts but commented these out.
If you check the Lookup.RetrieveIE line, all that does is return a dataset.  Of course behind the scenes it dynamically builds the select statement to query the database to put the results into a dataset.

Please keep in mind that I need to have paging and sorting on the datagrid.  Sorting, of which, I haven't been able to get to work properly.  If you could include the code for that I would appreciate it!
Thankyou in advance
Adrian
-------------------------------------------------------------------------------------------------------------------------------------
Dim dr As DataSet
Private Sub CreateGrid()
        Dim Lookup As New cLookup
           
            dr = Lookup.RetrieveIE(Request("JobID"))

' some formatting of the datagrid
        Me.dgIEOutput.BorderWidth = Unit.Pixel(2)
        dgIEOutput.CellPadding = 2
        dgIEOutput.GridLines = GridLines.Both
        dgIEOutput.BorderColor = Color.Black
        dgIEOutput.ShowHeader = True

        Me.dgIEOutput.AllowPaging = True
        Me.dgIEOutput.PagerStyle.Mode = PagerMode.NumericPages
        Me.dgIEOutput.AllowSorting = True
        'Me.dgIEOutput.Columns.Add(column)
        Dim dv As DataView = New DataView(dr.Tables(0))
        ' dv.Sort = "Name"

' the following is some poor attempts to add a column at the front
'dr.Tables(0).Columns.Add("Try")
        'Dim datagridcol = New HyperLinkColumn
        'datagridcol.HeaderText = "Add as Group"
        'datagridcol.DataNavigateUrlFormatString = "<a href='Job.aspx'>do it now</a>"
        'datagridcol.text = "<a href='JobGroup.aspx'>do it now</a>"
        'datagridcol.SortExpression = dr.GetName(iCol).ToString() & " DESC"


        'dr.Tables(0).Columns.Add(New DataColumn("IntegerValue", GetType(Integer)))
        'dr.Tables(0).Columns.Add(New DataColumn("StringValue", GetType(String)))

        'Dim hc1 As New HyperLinkColumn
        'hc1.DataTextField = "StringValue"
        'hc1.DataTextFormatString = "Show details for {0}"
        'hc1.DataNavigateUrlField = "IntegerValue"
        'hc1.DataNavigateUrlFormatString = "details.aspx?ID={0}"
        'hc1.HeaderText = "Show me more"

        'dgIEOutput.Columns.AddAt(0, datagridcol)
        'dgIEOutput.AutoGenerateColumns = True
        BindData()
        'Dim i As Integer
        'For i = 0 To dr.Tables(0).Rows.Count - 1
        '    dr.Tables(0).Rows(i).Item(0) = "sgdf"
        '    '    '    datagridcol.DataNavigateUrlFormatString = "JobGroup.aspx"
        'Next
end sub

Private Sub BindData()
        Me.dgIEOutput.DataSource = dr
        Me.dgIEOutput.DataBind()
    End Sub

'''''''''''' Here is the code I was using for the ItemDataBound event:
Private Sub dgIEOutput_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgIEOutput.ItemDataBound
        'Dim hypcol As HyperLinkColumn

        'hypcol = dgIEOutput.Columns(1)
        'hypcol.DataNavigateUrlFormatString = "JobGroup.aspx"
    End Sub
0
adrianroyce
Asked:
adrianroyce
  • 4
  • 3
  • 2
2 Solutions
 
adomsgCommented:
The best is u dont autogenerate the columns..
Below teaches u hw to add hyperlink and the url

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddinghyperlinkcolumnstodatagridwebcontrol.asp
0
 
adomsgCommented:
For sorting:
Set the allowSorting properties to TRUE in the datagrid.

Below is the code to add in VB.NET

Private Sub datagrid_SortCommand(ByVal sender As System.Object, ByVal e As DataGridSortCommandEventArgs) Handles datagrid.SortCommand

   session("searchOnly") = select * from table ORDER BY " + e.SortExpression
conn.open()
Dim myAdapter As New OleDbDataAdapter(Session("searchOnly"), Conn)
            myAdapter.Fill(datagrid)
            datagrid.DataSource = dstResult
            datagrid.DataBind()
            Conn.Close()
end sub
0
 
adrianroyceAuthor Commented:
adomsg
I put autogenerate columns on as there is an unknown quantity of columns retrieved from the datasource.
I cannot predict which columns the user will choose from the 50 or so columns.

Is there a better way than the way I have done it so far?

Adrian
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
adomsgCommented:
The sorting still can be used, but the hyperlink thing i am not very sure.
0
 
rovermCommented:
Why don't you use a TemplateColumn? See this example:
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

It includes the TemplateColumn and the Sorting issue.

D'Mzz!
RoverM
0
 
adrianroyceAuthor Commented:
I was hoping that someone could provide much more of a solution than the ones already provided.  It even seems some of you haven't read my entire request.  I am offering 500 points, if that's not enough, please let me know, I have more to spare.
Thanks
Adrian
0
 
adrianroyceAuthor Commented:
ok, I worked it out (great sigh of relief).
And yes I used a Template column, however I didn't realize that I could define a template column before hand with the Property Builder and then Bind the data and asp.net appends the columns to the end of the template column.
That's very cool.
Thankyou Roverm and adomsg.  I'll split the points between you guys.  I can use other ideas you've given me too.
0
 
rovermCommented:
Thanks! Glad we could help.
0
 
adomsgCommented:
Thanks...
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now