Changing an image in a datagrid column in the ItemDataBound event

M

MW

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
 
E

Eliyahu Goldin

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
 
M

MW

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,
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top