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

Discussion in 'ASP .Net Datagrid Control' started by QUASAR, Jan 14, 2004.

  1. QUASAR

    QUASAR Guest

    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! ;)
    QUASAR, Jan 14, 2004
    #1
    1. Advertising

  2. QUASAR

    Alvin Bruney Guest

    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" <> wrote in message
    news:...
    > 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! ;)
    >
    >
    Alvin Bruney, Jan 14, 2004
    #2
    1. Advertising

  3. QUASAR

    QUASAR Guest

    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" <vapor at steaming post office> ha scritto nel messaggio
    news:uR3$...
    > 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" <> wrote in message
    > news:...
    > > 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! ;)
    > >
    > >

    >
    >
    QUASAR, Jan 15, 2004
    #3
  4. QUASAR

    Alvin Bruney Guest

    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" <> wrote in message
    news:%...
    > 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" <vapor at steaming post office> ha scritto nel messaggio
    > news:uR3$...
    > > 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" <> wrote in message
    > > news:...
    > > > 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! ;)
    > > >
    > > >

    > >
    > >

    >
    >
    Alvin Bruney, Jan 15, 2004
    #4
  5. QUASAR

    QUASAR Guest

    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" <vapor at steaming post office> ha scritto nel messaggio
    news:ulT##...
    > 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" <> wrote in message
    > news:%...
    > > 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" <vapor at steaming post office> ha scritto nel messaggio
    > > news:uR3$...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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! ;)
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    QUASAR, Jan 16, 2004
    #5
  6. QUASAR

    QUASAR Guest

    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

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


    "QUASAR" <> ha scritto nel messaggio
    news:...
    > 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! ;)
    >
    >
    QUASAR, Jan 17, 2004
    #6
  7. QUASAR

    QUASAR Guest

    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

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

    Byez,
    Giorgio


    "QUASAR" <> ha scritto nel messaggio
    news:...
    > 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! ;)
    >
    >
    QUASAR, Jan 17, 2004
    #7
    1. Advertising

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. GJH
    Replies:
    0
    Views:
    97
  2. Joe Reiss

    Gridview - Accessing a field value in current row

    Joe Reiss, Jan 3, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    307
    Teemu Keiski
    Jan 4, 2006
  3. Chuck
    Replies:
    0
    Views:
    246
    Chuck
    Nov 10, 2003
  4. Cylix
    Replies:
    0
    Views:
    97
    Cylix
    Jun 13, 2006
  5. jr
    Replies:
    3
    Views:
    405
Loading...

Share This Page