Change style of a single row of the item list of datagrid, based on a field value of current item...

Q

QUASAR

Sorry for the long subject guys, but I don't know how better I can resume
the matter...

Anyway, I have my datagrid showing items of an order.
I want to have different backcolor of the rows based on the value of the
ItemType field of the current item (datagrid row)...
Example, if the current item (datagrid row) ItemType filed value is 1 i
wanna have this row backcolor red, if 2 then yellow, and so on...
Was very clear how to do this in classic asp but now using the datagrid
control nomore...
I know I can use a datalist that is more flexible, but I wanna use datagrid
:)

Thanks! ;)
 
A

Alvin Bruney

trap the itemdatabound event
if(e.item.listitemtype == listitemtype.item or alternateitem)
e.item.cells[0].BackColor = System.Drawing.Color.Red;

roughly
 
Q

QUASAR

Alvin thanks a lot.
I suppose the code given is C wich is not familiar to me (i'm a really newby
:) )
Can you detail a bit more in VB ?

Thanks,
Quasar ;)

Alvin Bruney said:
trap the itemdatabound event
if(e.item.listitemtype == listitemtype.item or alternateitem)
e.item.cells[0].BackColor = System.Drawing.Color.Red;

roughly

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
QUASAR said:
Sorry for the long subject guys, but I don't know how better I can resume
the matter...

Anyway, I have my datagrid showing items of an order.
I want to have different backcolor of the rows based on the value of the
ItemType field of the current item (datagrid row)...
Example, if the current item (datagrid row) ItemType filed value is 1 i
wanna have this row backcolor red, if 2 then yellow, and so on...
Was very clear how to do this in classic asp but now using the datagrid
control nomore...
I know I can use a datalist that is more flexible, but I wanna use datagrid
:)

Thanks! ;)
 
A

Alvin Bruney

from a scott mitchell article

Sub ItemDataBoundEventHandler(sender as Object, e as DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item OR _
e.Item.ItemType = ListItemType.AlternatingItem then
'Check to see if the price is below a certain threshold
Dim price as Double
price = Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "price"))

If price < 10.0 then
e.Item.BackColor = System.Drawing.Color.Yellow
End If
End If
End Sub


--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
QUASAR said:
Alvin thanks a lot.
I suppose the code given is C wich is not familiar to me (i'm a really newby
:) )
Can you detail a bit more in VB ?

Thanks,
Quasar ;)

Alvin Bruney said:
trap the itemdatabound event
if(e.item.listitemtype == listitemtype.item or alternateitem)
e.item.cells[0].BackColor = System.Drawing.Color.Red;

roughly

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
QUASAR said:
Sorry for the long subject guys, but I don't know how better I can resume
the matter...

Anyway, I have my datagrid showing items of an order.
I want to have different backcolor of the rows based on the value of the
ItemType field of the current item (datagrid row)...
Example, if the current item (datagrid row) ItemType filed value is 1 i
wanna have this row backcolor red, if 2 then yellow, and so on...
Was very clear how to do this in classic asp but now using the datagrid
control nomore...
I know I can use a datalist that is more flexible, but I wanna use datagrid
:)

Thanks! ;)
 
Q

QUASAR

Thanks Alvin, I've tried but doesn't work... I give you here my code for the
sub...
What goes wrong?
Can you give me a link to the article of Scott Mitchell you mention?

Thanks, Giorgio.
----------------------------------------------------------------------------
---------

Sub ItemDataBoundEventHandler(sender as Object, e as
DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item OR _
e.Item.ItemType = ListItemType.AlternatingItem then
'Check to see if the genre ID is 1 (Ancillary)
Dim genre As String
genre = (DataBinder.Eval(e.Item.DataItem, "_ID_genre"))

If genre > 1 then
e.Item.BackColor = System.Drawing.Color.Yellow
End If
End If
End Sub

'---------------------------------------------------------------------------
----------

Alvin Bruney said:
from a scott mitchell article

Sub ItemDataBoundEventHandler(sender as Object, e as DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item OR _
e.Item.ItemType = ListItemType.AlternatingItem then
'Check to see if the price is below a certain threshold
Dim price as Double
price = Convert.ToDouble(DataBinder.Eval(e.Item.DataItem, "price"))

If price < 10.0 then
e.Item.BackColor = System.Drawing.Color.Yellow
End If
End If
End Sub


--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
QUASAR said:
Alvin thanks a lot.
I suppose the code given is C wich is not familiar to me (i'm a really newby
:) )
Can you detail a bit more in VB ?

Thanks,
Quasar ;)

Alvin Bruney said:
trap the itemdatabound event
if(e.item.listitemtype == listitemtype.item or alternateitem)
e.item.cells[0].BackColor = System.Drawing.Color.Red;

roughly

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Sorry for the long subject guys, but I don't know how better I can resume
the matter...

Anyway, I have my datagrid showing items of an order.
I want to have different backcolor of the rows based on the value of the
ItemType field of the current item (datagrid row)...
Example, if the current item (datagrid row) ItemType filed value is
1
 
Q

QUASAR

Change color of a single row of the item list of datagrid, based on a field
value of current item...

Now I'm trying with this code but without success again :((
What is wrong?
No one can help???

Thanks :)

'---------------------------------------------------------------------------
Sub ItemGrid_ItemDataBound(ByVal source As Object, ByVal e As
DataGridItemEventArgs)
' OR ALSO I'VE TRYED Sub ItemGrid_ItemDataBound(sender as Object, e as
DataGridItemEventArgs)

If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem) Then

If e.Item.DataItem("_ID_genre") < 2 Then
' genre is "ancillary equipment"
e.Item.BackColor = System.Drawing.Color.Yellow
e.Item.Cells.Item(0).BackColor =
System.Drawing.Color.FromName("Green")
End If

End If

End Sub

'---------------------------------------------------------------------------
 
Q

QUASAR

Ok now it works...
was my fault, I've not setted in the datagrid properties the
"OnItemDataBound" event...


If someone may have the same need the final code was here...
Insert the sub and don't forgot to set in the datagrid properties the
"OnItemDataBound" event
<asp:DataGrid id="DataGrid1"...
....OnItemDataBound="DataGrid1_ItemDataBoundEvent"

'---------------------------------------------------------------------------
----------

Sub DataGrid1_ItemDataBoundEvent(sender as Object, e as
DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item _
OR e.Item.ItemType = ListItemType.AlternatingItem then

'Check to see if the genre ID is 1 (Ancillary)
If e.Item.DataItem("_ID_Tipo_Item") < 2 Then
e.Item.BackColor = System.Drawing.Color.FromName("Lavender")
End If

End If

End Sub

'---------------------------------------------------------------------------
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top