473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Creating Hyperlink in DataGrid Column

I'm using the DataGrid with AutoGenerateColumns set to false and choosing
which columns I want in the grid by using the <Columns> attribute. What I
want to do is to create a hyperlink out of one of the columns that I am
displaying, but in order to build this link, I need a value that is in my
SELECT clause, but is not being displayed via a BoundColumn.

Say I have a query: SELECT ID, Name FROM Companies

Here, ID is the PK. I am only displaying the Name column, but I want the
Name to be a hyperlink to a "details" page for the company. So, I want to
build a link such as "CompanyDetails.aspx?id=1234".

I know there is an event I can override to change how the Name cell in the
DataGrid is rendered. What I don't know is how I can programmatically
access the ID column from my query once I am in this event handler.

Thanks!
Nov 19 '05 #1
6 2928
Hi epigram,

You can use HyperLinkColumn. And create url in
datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType = ListItemType.Item )
{
DataRowView drv = (DataRowView)e.Item.DataItem;
TableCell nameCell = e.Item.Cells[nameIndex];
HyperLink link = (HyperLink) nameCell.Controls[0];
link.NavigateUrl = "CompanyDetails.aspx?id=" + drv
["ID"].ToString();
}

HTH

Elton Wang
el********@hotmail.com
-----Original Message-----
I'm using the DataGrid with AutoGenerateColumns set to false and choosingwhich columns I want in the grid by using the <Columns> attribute. What Iwant to do is to create a hyperlink out of one of the columns that I amdisplaying, but in order to build this link, I need a value that is in mySELECT clause, but is not being displayed via a BoundColumn.
Say I have a query: SELECT ID, Name FROM Companies

Here, ID is the PK. I am only displaying the Name column, but I want theName to be a hyperlink to a "details" page for the company. So, I want tobuild a link such as "CompanyDetails.aspx?id=1234".

I know there is an event I can override to change how the Name cell in theDataGrid is rendered. What I don't know is how I can programmaticallyaccess the ID column from my query once I am in this event handler.
Thanks!
.

Nov 19 '05 #2
A P
How about in using VB.NET?
"Elton Wang" <an*******@discussions.microsoft.com> wrote in message
news:14****************************@phx.gbl...
Hi epigram,

You can use HyperLinkColumn. And create url in
datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType = ListItemType.Item )
{
DataRowView drv = (DataRowView)e.Item.DataItem;
TableCell nameCell = e.Item.Cells[nameIndex];
HyperLink link = (HyperLink) nameCell.Controls[0];
link.NavigateUrl = "CompanyDetails.aspx?id=" + drv
["ID"].ToString();
}

HTH

Elton Wang
el********@hotmail.com
-----Original Message-----
I'm using the DataGrid with AutoGenerateColumns set to

false and choosing
which columns I want in the grid by using the <Columns>

attribute. What I
want to do is to create a hyperlink out of one of the

columns that I am
displaying, but in order to build this link, I need a

value that is in my
SELECT clause, but is not being displayed via a

BoundColumn.

Say I have a query: SELECT ID, Name FROM Companies

Here, ID is the PK. I am only displaying the Name

column, but I want the
Name to be a hyperlink to a "details" page for the

company. So, I want to
build a link such as "CompanyDetails.aspx?id=1234".

I know there is an event I can override to change how the

Name cell in the
DataGrid is rendered. What I don't know is how I can

programmatically
access the ID column from my query once I am in this

event handler.

Thanks!
.

Nov 19 '05 #3
Following is VB code

If e.Item.ItemType = ListItemType.Item OrElse
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem,
DataRowView)
Dim nameCell As TableCell = e.Item.Cells(nameIndex)
Dim link As HyperLink = CType(nameCell.Controls(0),
HyperLink)
link.NavigateUrl = "CompanyDetails.aspx?id=" + drv
("ID").ToString
End If

HTH

Elton Wang

-----Original Message-----
How about in using VB.NET?
"Elton Wang" <an*******@discussions.microsoft.com> wrote in messagenews:14****************************@phx.gbl...
Hi epigram,

You can use HyperLinkColumn. And create url in
datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType = ListItemType.Item )
{
DataRowView drv = (DataRowView)e.Item.DataItem;
TableCell nameCell = e.Item.Cells[nameIndex];
HyperLink link = (HyperLink) nameCell.Controls[0];
link.NavigateUrl = "CompanyDetails.aspx?id=" + drv
["ID"].ToString();
}

HTH

Elton Wang
el********@hotmail.com
>-----Original Message-----
>I'm using the DataGrid with AutoGenerateColumns set to

false and choosing
>which columns I want in the grid by using the <Columns>

attribute. What I
>want to do is to create a hyperlink out of one of the

columns that I am
>displaying, but in order to build this link, I need a

value that is in my
>SELECT clause, but is not being displayed via a

BoundColumn.
>
>Say I have a query: SELECT ID, Name FROM Companies
>
>Here, ID is the PK. I am only displaying the Name

column, but I want the
>Name to be a hyperlink to a "details" page for the

company. So, I want to
>build a link such as "CompanyDetails.aspx?id=1234".
>
>I know there is an event I can override to change how
the Name cell in the
>DataGrid is rendered. What I don't know is how I can

programmatically
>access the ID column from my query once I am in this

event handler.
>
>Thanks!
>
>
>.
>

.

Nov 19 '05 #4
Hi Elton,

I'm getting an error:

"System.ArgumentOutOfRangeException: Specified argument was out of the range
of valid values. Parameter name: index" with regard to the statement:

HyperLink link = (HyperLink) nameCell.Controls[0];

This cell is a BoundColumn. Any ideas what could be happening here?

Thanks.
"Elton Wang" <an*******@discussions.microsoft.com> wrote in message
news:14****************************@phx.gbl...
Hi epigram,

You can use HyperLinkColumn. And create url in
datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType = ListItemType.Item )
{
DataRowView drv = (DataRowView)e.Item.DataItem;
TableCell nameCell = e.Item.Cells[nameIndex];
HyperLink link = (HyperLink) nameCell.Controls[0];
link.NavigateUrl = "CompanyDetails.aspx?id=" + drv
["ID"].ToString();
}

HTH

Elton Wang
el********@hotmail.com
-----Original Message-----
I'm using the DataGrid with AutoGenerateColumns set to

false and choosing
which columns I want in the grid by using the <Columns>

attribute. What I
want to do is to create a hyperlink out of one of the

columns that I am
displaying, but in order to build this link, I need a

value that is in my
SELECT clause, but is not being displayed via a

BoundColumn.

Say I have a query: SELECT ID, Name FROM Companies

Here, ID is the PK. I am only displaying the Name

column, but I want the
Name to be a hyperlink to a "details" page for the

company. So, I want to
build a link such as "CompanyDetails.aspx?id=1234".

I know there is an event I can override to change how the

Name cell in the
DataGrid is rendered. What I don't know is how I can

programmatically
access the ID column from my query once I am in this

event handler.

Thanks!
.

Nov 19 '05 #5
As I mentioned you should make the column as
HyperLinkColumn rather than BoundColumn. It's like

<Columns>
<asp:HyperLinkColumn DataTextField="Name"
HeaderText="Name"></asp:HyperLinkColumn>
other columns, in any
</Columns>

BTW, you should also use datatable, (or dataview, or
dataset) as datagrid's data source. Otherwise
DataRowView drv = (DataRowView)e.Item.DataItem

doesn't work.

HTH

Elton Wang

-----Original Message-----
Hi Elton,

I'm getting an error:

"System.ArgumentOutOfRangeException: Specified argument was out of the rangeof valid values. Parameter name: index" with regard to the statement:
HyperLink link = (HyperLink) nameCell.Controls[0];

This cell is a BoundColumn. Any ideas what could be happening here?
Thanks.
"Elton Wang" <an*******@discussions.microsoft.com> wrote in messagenews:14****************************@phx.gbl...
Hi epigram,

You can use HyperLinkColumn. And create url in
datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType = ListItemType.Item )
{
DataRowView drv = (DataRowView)e.Item.DataItem;
TableCell nameCell = e.Item.Cells[nameIndex];
HyperLink link = (HyperLink) nameCell.Controls[0];
link.NavigateUrl = "CompanyDetails.aspx?id=" + drv
["ID"].ToString();
}

HTH

Elton Wang
el********@hotmail.com
-----Original Message-----
I'm using the DataGrid with AutoGenerateColumns set to

false and choosing
which columns I want in the grid by using the <Columns>

attribute. What I
want to do is to create a hyperlink out of one of the

columns that I am
displaying, but in order to build this link, I need a

value that is in my
SELECT clause, but is not being displayed via a

BoundColumn.

Say I have a query: SELECT ID, Name FROM Companies

Here, ID is the PK. I am only displaying the Name

column, but I want the
Name to be a hyperlink to a "details" page for the

company. So, I want to
build a link such as "CompanyDetails.aspx?id=1234".

I know there is an event I can override to change how
the Name cell in the
DataGrid is rendered. What I don't know is how I can

programmatically
access the ID column from my query once I am in this

event handler.

Thanks!
.

.

Nov 19 '05 #6
As I mentioned you should make the column as
HyperLinkColumn rather than BoundColumn. It's like

<Columns>
<asp:HyperLinkColumn DataTextField="Name"
HeaderText="Name"></asp:HyperLinkColumn>
other columns, in any
</Columns>

BTW, you should also use datatable, (or dataview, or
dataset) as datagrid's data source. Otherwise
DataRowView drv = (DataRowView)e.Item.DataItem

doesn't work.

HTH

Elton Wang

-----Original Message-----
Hi Elton,

I'm getting an error:

"System.ArgumentOutOfRangeException: Specified argument was out of the rangeof valid values. Parameter name: index" with regard to the statement:
HyperLink link = (HyperLink) nameCell.Controls[0];

This cell is a BoundColumn. Any ideas what could be happening here?
Thanks.
"Elton Wang" <an*******@discussions.microsoft.com> wrote in messagenews:14****************************@phx.gbl...
Hi epigram,

You can use HyperLinkColumn. And create url in
datagrid_ItemDataBound event:

if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType = ListItemType.Item )
{
DataRowView drv = (DataRowView)e.Item.DataItem;
TableCell nameCell = e.Item.Cells[nameIndex];
HyperLink link = (HyperLink) nameCell.Controls[0];
link.NavigateUrl = "CompanyDetails.aspx?id=" + drv
["ID"].ToString();
}

HTH

Elton Wang
el********@hotmail.com
-----Original Message-----
I'm using the DataGrid with AutoGenerateColumns set to

false and choosing
which columns I want in the grid by using the <Columns>

attribute. What I
want to do is to create a hyperlink out of one of the

columns that I am
displaying, but in order to build this link, I need a

value that is in my
SELECT clause, but is not being displayed via a

BoundColumn.

Say I have a query: SELECT ID, Name FROM Companies

Here, ID is the PK. I am only displaying the Name

column, but I want the
Name to be a hyperlink to a "details" page for the

company. So, I want to
build a link such as "CompanyDetails.aspx?id=1234".

I know there is an event I can override to change how
the Name cell in the
DataGrid is rendered. What I don't know is how I can

programmatically
access the ID column from my query once I am in this

event handler.

Thanks!
.

.

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ravikanth[MVP] | last post by:
Hi asp:DataGrid id="YourID" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="Sample Column"> <ItemTemplate> <asp:Hyperlink runat="server" Text='<%...
2
by: damonf | last post by:
I'm currently trying to add an ASP hyperlink to a template column in a datagrid. The normal hyperlink column doesn't give me the ability to add attributes to the item. In my grid there are four...
2
by: Alex | last post by:
I would like to have one column in my DataGrid that content acts as a hyperlink. In addition to that I would like that the content is databound to a DataSet source. If I create a hyperlink...
9
by: Paul | last post by:
Hi I have a data grid with a hyperlink column. the colum has numbers like 00001,000002, ect. Just wondering how to get the text value of the cell as tempstring =...
3
by: Tim::.. | last post by:
I currently have the following datagrid but want to turn the name and email column into a hyperlink in the codebehind! Can someone please tell me how I achieve this! Thanks Private Sub...
10
by: david | last post by:
Hi, all: I need a help from you about DataGrid control. I created a DataGrid, dg, in design view of .NET visual Stadio and use the builder to add a Hyperlink column to dg. I want to try to assign...
19
by: Joe | last post by:
I have an aspx page (referred to here as page_1) with a datagrid whose first column contains hyperlinks. When a user clicks one of these hyperlinks, he will navigate to another aspx page (referred...
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.