Dynamically setting datagrid column in code-behind?

Discussion in 'ASP .Net' started by Roy, Jul 25, 2005.

  1. Roy

    Roy Guest

    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.
     
    Roy, Jul 25, 2005
    #1
    1. Advertisements

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

    "Roy" <> wrote in message
    news:...
    > 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.
    >
     
    Grant Merwitz, Jul 25, 2005
    #2
    1. Advertisements

  3. 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" <> wrote in message
    news:...
    > 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.
    >
     
    Eliyahu Goldin, Jul 25, 2005
    #3
  4. Hi Roy,

    The simple way is to use HyperLinkColumn:

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

    HTH

    Elton



    "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.
    >
    >
     
    =?Utf-8?B?RWx0b24gVw==?=, Jul 25, 2005
    #4
  5. Roy

    Roy Guest

    Roy, Jul 25, 2005
    #5
  6. Roy

    Roy Guest

    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 ***
     
    Roy, Jul 25, 2005
    #6
  7. Roy

    Roy Guest

    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 ***
     
    Roy, Jul 25, 2005
    #7
  8. 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" <> wrote in message
    news:%...
    > 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 ***
     
    Eliyahu Goldin, Jul 25, 2005
    #8
  9. 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 ***
    >
     
    =?Utf-8?B?RWx0b24gVw==?=, Jul 25, 2005
    #9
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    744
    Alphonse Giambrone
    Jun 28, 2003
  2. Charleees
    Replies:
    2
    Views:
    8,394
    Eliyahu Goldin
    Jun 20, 2006
  3. Replies:
    0
    Views:
    535
  4. Replies:
    2
    Views:
    1,246
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    621
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...

Share This Page