Dynamically setting datagrid column in code-behind?

R

Roy

Hey all,

On my html page I have a datagrid with the column:
<boundColumn datafield="xyz" visible = false>
</boundColumn>

In my code behind, within item data bound event, I dynamically set the
column text like so:
e.item.cells(10).text = mystring.trim

Why I'm doing this is because I'm essentially using the invisible
boundcolumn to store info for use by this datagrid column's hyperlink:

<templatecolumn>
<itemtemplate>
<hyperlink navigateurl="dnl.aspx?a=xyz"

e.item.cells(10) is being set properly and contains the data (or as
best as I can tell). However, the templatecolumn's hyperlink never
changes and always displays the default data in the link.
It's almost as if the hyperlink info gets set prior to the boundcolumn.
How can I get around this?

Thanks.
 
G

Grant Merwitz

You can use the OnItemDataBound event.

if you set an ID for you HyperLink (or use its ordinal position), you can
modify from the code behind.

e.g.

private void DataGrid1_OnItemDataBound(object Sender,
System.Data.DataItemEventArgs e)
{
if(e.Item.ItemTemplate != ListItemType.Header && e.Item.ItemTemplate !=
ListItemType.Footer)
{
((Hyperlink)e.Item.FindControl("Hyperlink1")).NavigateUrl =
"dnl.aspx?a=" + [value goes here]; //This could be e.Item.Cells[0].Text for
example
}
}

hope that helps
 
E

Eliyahu Goldin

If all you need is just to use the value of another field in the datarecord
without displaying it, you don't need to allocate a datagrid column for
that. Use property DataItem of the datagrid item in ItemDataBound event.
Typecast it to you record type and get the field value from there.

Eliyahu
 
G

Guest

Hi Roy,

The simple way is to use HyperLinkColumn:

<asp:HyperLinkColumn DataTextField="xyz" DataNavigateUrlField="xyz"
DataNavigateUrlFormatString="dnl.aspx?a={0}" ></asp:HyperLinkColumn>

HTH

Elton
(e-mail address removed)
 
R

Roy

Thanks for the post Eliyahu, I only wish it were so easy. :)

The field value is external to the datagrid.
 
R

Roy

Thanks for the post Elton, but the hyperlinkcolumn can only pass single
parameters. I need to pass 5, hence, the templatecolumn.
 
E

Eliyahu Goldin

I am afraid you missed the point. DataItem property refers to the data
record used for populating the grid. The record is not a part of the grid.
It is what the grid gets column values from.

Eliyahu
 
G

Guest

In datagrid_ItemDataBound event, you can also rebiuld hyperlinkcolumn's url
and pass 5 parameters to it.

HTH

Elton
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top