Solved

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

Posted on 2003-11-23
9
1,594 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Assisted Solution

by:adomsg
adomsg earned 250 total points
Comment Utility
The sorting still can be used, but the hyperlink thing i am not very sure.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Accepted Solution

by:
roverm earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks! Glad we could help.
0
 
LVL 2

Expert Comment

by:adomsg
Comment Utility
Thanks...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

10 Experts available now in Live!

Get 1:1 Help Now