Help with DataGrid

dev2dev

Newcomer
Joined
Jan 4, 2005
Messages
15
Hi, I am showing Tasks of employee,
I want to add an image which will be hidden and should be visible when task priority is high.

How to achieve this?

I added a template control to which I added an image control and a lable.
In aspx, i added code like this
Code:
											<asp:TemplateColumn>
												<ItemTemplate>
													<asp:Image id="imgPriority" runat="server" Visible="False" ImageUrl="../images/high_priority.gif"></asp:Image>
													<asp:Label id=lblPriority runat="server" Text='<%# DataBinder.Eval(Container, "priority") %>'>
													</asp:Label>
												</ItemTemplate>
											</asp:TemplateColumn>

I am getting error at the DataBinder.Eval line saying the "'System.Web.UI.WebControls.DataGridItem' does not contain a property with the name priority.'

But I am sure its in my view

thank you
 
I forgot the actual question.
Okey, I got it after few trials of compilations.

How to change properties of the image control which is in my template control of my datagrid.

I remember, i read somewhere, we should handle it in grid's onitemcreate event

but no clue how

thank you
 
Hi,
I am back, I found a sample code in the MSDN.com

I tried this
Code:
        Private Sub grdTask_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdTask.ItemCreated
            If e.Item.ItemType = ListItemType.Item Then
                Dim lbl As Label
                lbl = CType(grdTask.FindControl("lblPriority"), Label)
                If lbl.Text = "High" Then
                    Dim img As System.Web.UI.WebControls.Image
                    img = CType(grdTask.FindControl("imgPriority"), System.Web.UI.WebControls.Image)
                    img.Visible = True
                End If
            End If
        End Sub

Giving me error at lbl.Text, saying instance not set
 
Hi, I just changed the code
Code:
            If e.Item.ItemType = ListItemType.Item Then
                Dim lbl As Label
                lbl = CType(e.Item.FindControl("lblPriority"), Label)
                Dim img As System.Web.UI.WebControls.Image
                img = CType(e.Item.FindControl("imgPriority"), System.Web.UI.WebControls.Image)
                If lbl.Text = "High" Then
                    img.Visible = True
                Else
                    img.Visible = False
                End If
            End If

surprisingly when this code was in itemcreated event, then each event(row) lbl.text is showing empty.

then i moved the code to databound event of datagrid then it started showing "High" for all events.

and also, my dataview contains only two records where as this event is firing 3 times.

I am tired now, already wasted around 4 hours of time

I will back after you experts give me some suggestions

thank you
 
Why you are filling the text of the label in the .aspx page then checking for it in your itemdatabound event? Why not check for "priority" in your itemdatabound event then set the Label and image properties accordingly.
 
kahlua001 said:
Why you are filling the text of the label in the .aspx page then checking for it in your itemdatabound event? Why not check for "priority" in your itemdatabound event then set the Label and image properties accordingly.

thanks kahulua, in my last post i mentioned the same thing, i moved the comde from itemcreated event to itembound event, their i am getting values as "high" for all records and also,
i found its firing 3 times, where as i have only two records

thank you
 
Can you post your most recent code. Also, you should check for e.item.itemypte = alternatingitem too. Also post a sample of the data you are expecting. how do you know its firing 3 times instead of the expected 2?
 
This my code
Code:
        Private Sub grdTask_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdTask.ItemDataBound
            If e.Item.ItemType = ListItemType.Item Then
                Dim lbl As Label
                lbl = CType(e.Item.FindControl("lblPriority"), Label)
                Dim img As System.Web.UI.WebControls.Image
                img = CType(e.Item.FindControl("imgPriority"), System.Web.UI.WebControls.Image)
                If lbl.Text = "High" Then
                    img.Visible = True
                Else
                    img.Visible = False
                End If
            End If
        End Sub

how do you know its firing 3 times instead of the expected 2?
Debugging, I have set BreakPoint at first line after the If Condition (if itemtype is item)
Where I am seeing the event firing 3 times

thank you
 
Not sure if this will help, but also check for e.item.itemypte = alternatingitem

Code:
If e.item.itemtype =  ListItemType.Item or e.item.itemtype = ListItemType.AlternatingItem then
...
end if

lets start here.
 
Back
Top