Solved

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

Posted on 2003-11-23
9
1,600 Views
Last Modified: 2012-05-04
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
Comment
Question by:adrianroyce
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:adomsg
ID: 9808180
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
 
LVL 2

Expert Comment

by:adomsg
ID: 9808196
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
 

Author Comment

by:adrianroyce
ID: 9808259
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Assisted Solution

by:adomsg
adomsg earned 250 total points
ID: 9808890
The sorting still can be used, but the hyperlink thing i am not very sure.
0
 
LVL 12

Accepted Solution

by:
roverm earned 250 total points
ID: 9808985
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
 

Author Comment

by:adrianroyce
ID: 9815808
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
 

Author Comment

by:adrianroyce
ID: 9815867
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
 
LVL 12

Expert Comment

by:roverm
ID: 9816346
Thanks! Glad we could help.
0
 
LVL 2

Expert Comment

by:adomsg
ID: 9821948
Thanks...
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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