Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-11-23
9
Medium Priority
?
1,620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 2

Assisted Solution

by:adomsg
adomsg earned 750 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 750 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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