Set the background colour of a grid

Discussion in 'ASP .Net' started by John Doe, Oct 21, 2003.

  1. John Doe

    John Doe Guest

    Hi
    Is it possible to set the background colour of a grid
    based on a value in data?
    I guess that it is simple but I have been unable to figure
    it out.

    TIA
     
    John Doe, Oct 21, 2003
    #1
    1. Advertisements

  2. John Doe

    Alvin Bruney Guest

    What you want is the BackColor property on the DataGrid object. It takes a
    Color enum
    Color.Red for example

    regards
     
    Alvin Bruney, Oct 21, 2003
    #2
    1. Advertisements

  3. John Doe

    John Doe Guest

    Hi Alvin

    I am just learning this, I have a grid that has a
    DataTable as the source.

    I have created the following function:
    private void itemcreated(object sender,
    DataGridItemEventArgs e)
    {
    if(((DataRowView).e.Item.DataItem).ItemArray[5].ToString
    () == "0")
    {
    e.Item.Cells[5].BackColor = System.Drawing.Color.Red;
    }
    }

    I have then specified that the datagrid should use this
    function when OnItemCreated is fired.

    This gives me an access violation that I guess is due to
    the fact that I have a DataTable as the source. I think
    that I am on the right track since removing the if
    statement colors the column red as I want.

    TIA
     
    John Doe, Oct 21, 2003
    #3
  4. John Doe

    John Doe Guest

    Solved it myself. Used the following code:

    private void Item_Bound(object sender,
    DataGridItemEventArgs e)
    {
    if ((e.Item.ItemType == ListItemType.Item) ||
    (e.Item.ItemType == ListItemType.AlternatingItem))
    {
    if(e.Item.Cells[5].Text !="0")
    {
    e.Item.Cells[5].BackColor = System.Drawing.Color.Red;
    }
    }
    }

    Then I specified the following property on the datagrid
    OnItemDataBound="Item_Bound"

     
    John Doe, Oct 21, 2003
    #4
  5. John Doe

    Alvin Bruney Guest

    substitute for the if part:
    if(e.Item.Cells[5].Text = "0")
    //color cell

    --


    -----------
    Got TidBits?
    Get it here: www.networkip.net/tidbits/default.htm

     
    Alvin Bruney, Oct 21, 2003
    #5
  6. John Doe

    John Doe Guest

    Thanks Alvin
     
    John Doe, Oct 21, 2003
    #6
  7. John Doe

    John Doe Guest

    Sorry for my silly question. But how do I loop through
    each cell in the cells collection and set the background
    colour.
     
    John Doe, Oct 21, 2003
    #7
  8. John Doe

    Alvin Bruney Guest

    for(int i = 0; i < e.item.cells.count;i++)
    e.items.cells.BackColor = Color.red;


    roughly
     
    Alvin Bruney, Oct 22, 2003
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.