Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamically setting datagrid column in code-behind?

Reply
Thread Tools

Dynamically setting datagrid column in code-behind?

 
 
Roy
Guest
Posts: n/a
 
      07-25-2005
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.

 
Reply With Quote
 
 
 
 
Grant Merwitz
Guest
Posts: n/a
 
      07-25-2005
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")).Navi gateUrl =
"dnl.aspx?a=" + [value goes here]; //This could be e.Item.Cells[0].Text for
example
}
}

hope that helps

"Roy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
>



 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      07-25-2005
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

"Roy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
>



 
Reply With Quote
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      07-25-2005
Hi Roy,

The simple way is to use HyperLinkColumn:

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

HTH

Elton
http://www.velocityreviews.com/forums/(E-Mail Removed)


"Roy" wrote:

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

 
Reply With Quote
 
Roy
Guest
Posts: n/a
 
      07-25-2005
Thanks for the tip Grant, I'll give it a whirl and get back to you.



*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Roy
Guest
Posts: n/a
 
      07-25-2005
Thanks for the post Eliyahu, I only wish it were so easy.

The field value is external to the datagrid.


*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Roy
Guest
Posts: n/a
 
      07-25-2005
Thanks for the post Elton, but the hyperlinkcolumn can only pass single
parameters. I need to pass 5, hence, the templatecolumn.

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      07-25-2005
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

"Roy" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Thanks for the post Eliyahu, I only wish it were so easy.
>
> The field value is external to the datagrid.
>
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      07-25-2005
In datagrid_ItemDataBound event, you can also rebiuld hyperlinkcolumn's url
and pass 5 parameters to it.

HTH

Elton

"Roy" wrote:

> Thanks for the post Elton, but the hyperlinkcolumn can only pass single
> parameters. I need to pass 5, hence, the templatecolumn.
>
> *** Sent via Developersdex http://www.developersdex.com ***
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView - Setting the text of the 'select' column to the value of another column Greg Smith ASP .Net 0 04-12-2007 03:11 PM
GridView - Setting the text of the 'select' column to the value of another column Greg Smith ASP .Net Web Controls 0 04-12-2007 02:40 PM
setting Column width of Template Column in Datagrid.. Charleees ASP .Net 2 06-20-2006 12:52 PM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
how do i access a dropdownlists selected value in a datagrid edititemtemplate column from the selectedindexchanged event of another dropdownlist in a datagrid edititemtemplate column Dave M ASP .Net Datagrid Control 0 12-14-2004 11:53 AM



Advertisments