Changing an image in a datagrid column in the ItemDataBound event

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Changing an image in a datagrid column in the ItemDataBound event

    Hi,

    I have a datagrid that is bound to a datatable, I'm using
    TemplateColumns.

    The heading of two columns of the datagrid fire the
    ItemCommand event in which I sort the datatable then
    rebind to the datagrid. One column is an image (which
    the imageurl is set in the first binding of the grid).

    On the ItemDataBound event after the sort when the
    datatable is re-binded to the grid, I go through the
    datatable because I want to change the image based on the
    column value (which I know is sorting properly), but the
    images aren't changing. I can't seem to figure out why.

    on the aspx page:
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Image Runat="server" />
    </ItemTemplate>
    </asp:TemplateColumn>

    in the code behind (ItemDataBound event):
    Dim bolClosed as Boolean

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType
    = ListItemType.AlternatingItem Then
    bolClosed = CType(myDataTable.Rows
    (e.Item.ItemIndex).Item("Closed"), Boolean)

    If bolClosed Then
    imgStatus = CType(e.Item.Cells(1).Controls
    (1), WebControls.Image)
    imgStatus.ImageUrl =
    Request.ApplicationPath & "/Images/closed.gif"
    Else
    imgStatus = CType(e.Item.Cells(1).Controls
    (1), WebControls.Image)
    imgStatus.ImageUrl =
    Request.ApplicationPath & "/Images/open.gif"
    End If
    End If
    MW Guest

  2. Similar Questions and Discussions

    1. ItemDataBound Event - How to access the previous record when this event is raised in DataGrid?
      ItemDataBound Event - How to access the previous record when this event is raised in DataGrid? During "ItemDataBound Event", I would like...
    2. 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
      i am trying to trap the value of one dropdownlist (in a datagrid edititemtemplate column) at the time its selectedindexchanged event fires and use...
    3. formating column in the ItemDataBound event
      Ken wrote: Ken, check out this example: http://tinyurl.com/4dubw -- Scott Mitchell mitchell@4guysfromrolla.com...
    4. Changing ImageURL of Image Button which is added in template column of datagrid
      hello, I have a template column in my datagrid. To the header of template colum, i have added imagebutton. On click of the imagebutton , i...
    5. getting column value in ItemCreated datagrid event
      got it. use DataItem("columnname"( "Learning SQL Server" <no.mail.com> wrote in message news:ulsj5VNYDHA.4040@tk2msftngp13.phx.gbl......
  3. #2

    Default Re: Changing an image in a datagrid column in the ItemDataBound event

    Does the code set bolClosed correctly to true/false depending on the
    "Closed" field content?

    BTW, why are you using myDataTable.Rows (e.Item.ItemIndex).Item("Closed")
    instead of simple e.Item("Closed")?

    Eliyahu

    "MW" <anonymous@discussions.microsoft.com> wrote in message
    news:791d01c49511$e11e79b0$a501280a@phx.gbl...
    > Hi,
    >
    > I have a datagrid that is bound to a datatable, I'm using
    > TemplateColumns.
    >
    > The heading of two columns of the datagrid fire the
    > ItemCommand event in which I sort the datatable then
    > rebind to the datagrid. One column is an image (which
    > the imageurl is set in the first binding of the grid).
    >
    > On the ItemDataBound event after the sort when the
    > datatable is re-binded to the grid, I go through the
    > datatable because I want to change the image based on the
    > column value (which I know is sorting properly), but the
    > images aren't changing. I can't seem to figure out why.
    >
    > on the aspx page:
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:Image Runat="server" />
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    > in the code behind (ItemDataBound event):
    > Dim bolClosed as Boolean
    >
    > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType
    > = ListItemType.AlternatingItem Then
    > bolClosed = CType(myDataTable.Rows
    > (e.Item.ItemIndex).Item("Closed"), Boolean)
    >
    > If bolClosed Then
    > imgStatus = CType(e.Item.Cells(1).Controls
    > (1), WebControls.Image)
    > imgStatus.ImageUrl =
    > Request.ApplicationPath & "/Images/closed.gif"
    > Else
    > imgStatus = CType(e.Item.Cells(1).Controls
    > (1), WebControls.Image)
    > imgStatus.ImageUrl =
    > Request.ApplicationPath & "/Images/open.gif"
    > End If
    > End If

    Eliyahu Goldin Guest

  4. #3

    Default Re: Changing an image in a datagrid column in the ItemDataBound event

    Hi Eliyau,

    thanks for the response. I was able to solve my problem
    based on what you told me. I couldn't use e.Item
    ("Closed"), but e.Item.DataItem("Closed") gave me the
    proper value.

    There is a difference between what myDataTable.Rows
    (e.Item.ItemIndex).Item("Closed") returns as a value and
    what e.Item.DataItem("Closed") returns.

    So, the difference must be because the View of the
    datatable is sorted, not the datatable?

    Thanks,
    >-----Original Message-----
    >Does the code set bolClosed correctly to true/false
    depending on the
    >"Closed" field content?
    >
    >BTW, why are you using myDataTable.Rows
    (e.Item.ItemIndex).Item("Closed")
    >instead of simple e.Item("Closed")?
    >
    >Eliyahu
    >
    >"MW" <anonymous@discussions.microsoft.com> wrote in
    message
    >news:791d01c49511$e11e79b0$a501280a@phx.gbl...
    >> Hi,
    >>
    >> I have a datagrid that is bound to a datatable, I'm
    using
    >> TemplateColumns.
    >>
    >> The heading of two columns of the datagrid fire the
    >> ItemCommand event in which I sort the datatable then
    >> rebind to the datagrid. One column is an image (which
    >> the imageurl is set in the first binding of the grid).
    >>
    >> On the ItemDataBound event after the sort when the
    >> datatable is re-binded to the grid, I go through the
    >> datatable because I want to change the image based on
    the
    >> column value (which I know is sorting properly), but
    the
    >> images aren't changing. I can't seem to figure out
    why.
    >>
    >> on the aspx page:
    >> <asp:TemplateColumn>
    >> <ItemTemplate>
    >> <asp:Image Runat="server" />
    >> </ItemTemplate>
    >> </asp:TemplateColumn>
    >>
    >> in the code behind (ItemDataBound event):
    >> Dim bolClosed as Boolean
    >>
    >> If e.Item.ItemType = ListItemType.Item Or
    e.Item.ItemType
    >> = ListItemType.AlternatingItem Then
    >> bolClosed = CType(myDataTable.Rows
    >> (e.Item.ItemIndex).Item("Closed"), Boolean)
    >>
    >> If bolClosed Then
    >> imgStatus = CType(e.Item.Cells
    (1).Controls
    >> (1), WebControls.Image)
    >> imgStatus.ImageUrl =
    >> Request.ApplicationPath & "/Images/closed.gif"
    >> Else
    >> imgStatus = CType(e.Item.Cells
    (1).Controls
    >> (1), WebControls.Image)
    >> imgStatus.ImageUrl =
    >> Request.ApplicationPath & "/Images/open.gif"
    >> End If
    >> End If
    >
    >
    >.
    >
    MW Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139