Creating Hyperlink in DataGrid Column

E

epigram

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!
 
E

Elton Wang

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
(e-mail address removed)
 
A

A P

How about in using VB.NET?


Elton Wang said:
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
(e-mail address removed)
-----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!


.
 
E

Elton Wang

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?


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
(e-mail address removed)
-----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!


.


.
 
E

epigram

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 said:
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
(e-mail address removed)
-----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!


.
 
E

Elton Wang

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 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.


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
(e-mail address removed)
-----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!


.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top