Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: Coloring the cell of a GridView based on Condition [VS 2008 - ASP.NET/C#]

Results 1 to 3 of 3
Share This Thread →
  1. #1
    Shaitan00 is offline Registered User
    Join Date
    Jan 2005
    Posts
    29
    Rep Power
    0

    Question Coloring the cell of a GridView based on Condition [VS 2008 - ASP.NET/C#]

    I am trying to find a way to display certain cells in RED or YELLOW based on specific criteria, ideally the cell itself could change color (it has to be very apparant) but in the worst-case a method of simply changing the color of the text would do - if there is no way to change the cell color.

    Specifically, I have a DataSet with a table like the following:
    [Date]-------[Type]--[Value]
    11/20/2008---HEP-----10.1
    11/20/2008---HAP-----7.8
    11/21/2008---HEP-----12
    11/21/2008---HAP-----100
    11/21/2008---AAH-----2
    (as an example)

    Currently I get the DataSet (SQL table) and then set it as a datasource to my GridView on my ASP.NET web page - this works great.

    Now I need to make the following changes:
    - If [VALUE] is greater then 99 set the cell in question RED
    - If [VALUE] is less then 10 set the cell in question YELLOW

    I've got absolutly no clue how to accomplish this...
    Is GridView the best method of displaying the data and setting colors? Is there an easy way to iterate over the data and determine which should be what color, is there a simpler approach?

    Any help would be greatly appreciated.
    Thanks,

  2. #2
    master_key's Avatar
    master_key is offline Contributing User
    Join Date
    Dec 2003
    Location
    India
    Posts
    698
    Rep Power
    106

    Thumbs up

    Use RowDataBound event of Gridview to accomplish this. You can change the color of cells by just changing it's CSS class if you want it CSS driven or you can write inline CSS for this.

    Hope this helps.

    ASP.NET Geeks
    Visit this group

  3. #3
    viplort is offline Contributing User
    Join Date
    Jan 2007
    Posts
    315
    Rep Power
    73
    Quote Originally Posted by Shaitan00
    I am trying to find a way to display certain cells in RED or YELLOW based on specific criteria, ideally the cell itself could change color (it has to be very apparant) but in the worst-case a method of simply changing the color of the text would do - if there is no way to change the cell color.

    Specifically, I have a DataSet with a table like the following:
    [Date]-------[Type]--[Value]
    11/20/2008---HEP-----10.1
    11/20/2008---HAP-----7.8
    11/21/2008---HEP-----12
    11/21/2008---HAP-----100
    11/21/2008---AAH-----2
    (as an example)

    Currently I get the DataSet (SQL table) and then set it as a datasource to my GridView on my ASP.NET web page - this works great.

    Now I need to make the following changes:
    - If [VALUE] is greater then 99 set the cell in question RED
    - If [VALUE] is less then 10 set the cell in question YELLOW

    I've got absolutly no clue how to accomplish this...
    Is GridView the best method of displaying the data and setting colors? Is there an easy way to iterate over the data and determine which should be what color, is there a simpler approach?

    Any help would be greatly appreciated.
    Thanks,
    Use something like the following in the rowdatabound event of your gridview:

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

    if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "num2Column")) > 99)
    {
    //change your row background color
    e.Row.BackColor = System.Drawing.Color.Gray;
    //change specific row cell background colors
    e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
    e.Row.Cells[2].ForeColor = System.Drawing.Color.Blue; ;
    }

    }

    }

Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. Ask for the <td> of a specific row and column
    By Dragon Rider in forum HTML, JavaScript And CSS Help
    Replies: 7
    Last Post: August 22nd, 2006, 12:57 PM
  2. Replies: 0
    Last Post: June 1st, 2006, 04:08 AM
  3. how do i autofit content to the table
    By aditya1_in in forum HTML, JavaScript And CSS Help
    Replies: 16
    Last Post: February 22nd, 2005, 03:54 PM
  4. Replies: 0
    Last Post: November 3rd, 2004, 11:25 AM
  5. Change datagrid cell color when a condition met?
    By dmoonme in forum ASP Development
    Replies: 2
    Last Post: August 5th, 2004, 12:54 PM

ASP Free Advertisers and Affiliates