Solved

gridview change cell backcolor based on variable

Posted on 2008-10-26
6
597 Views
Last Modified: 2013-12-17
I have a gridview in C# (2005/.Net2.0) and I would like to change the bg color of a cell (or the text color I really don't care at this point) based on the value of a variable.

If the cell falls in the 6th column, called "Age", and the value of a particular cell is above the average age ( double varAvgAge) then I would like the bg color to be Red. If the value of a cell is less than the average age, I would like the color to be green.

here is the code for my gridview:

        DataSet usersDataSet;
        string filePath = Server.MapPath("xml/averages.xml");
        usersDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        usersDataSet.ReadXml(filePath);
        GridView1.DataSource = usersDataSet.Tables[0].DefaultView;
        GridView1.DataBind();

thanks in advance
0
Comment
Question by:mackoner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 22809227
something like this
 
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int age = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Age"));
       
            if (age == 1)
            {
                e.Row.Cells[6].BackColor = System.Drawing.Color.Aqua;
            }
        }
    }
0
 

Author Comment

by:mackoner
ID: 22809502
thanks for the help.

I still couldn't quite get the color to change based on my variable varAvgAge. varAvgAge is a double, and is assigned a value from a calculation derived from data from the xml file.

I have my gridview set up on pageload
DataSet usersDataSet;
        string filePath = Server.MapPath("xml/average.xml");
        usersDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        usersDataSet.ReadXml(filePath);
        GridView1.DataSource = usersDataSet.Tables[0].DefaultView;
        GridView1.DataBind();
0
 

Author Comment

by:mackoner
ID: 22810117
i am not sure if I have to loop thru each record to find it's value for age and THEN do some sort of compairison??
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:mackoner
ID: 22810418
Also, a bit of sorting going on in the GridView:

            DataSet usersDataSet;
            string filePath = Server.MapPath("xml/average.xml");
            usersDataSet = new DataSet();
            //Read the contents of the XML file into the DataSet
            usersDataSet.ReadXml(filePath);
           
            DataView dv = usersDataSet.Tables[0].DefaultView;
            dv.Sort = "company, name, age ASC";
     
            GridView1.DataSource = dv;
            GridView1.DataBind();
0
 

Author Closing Comment

by:mackoner
ID: 31510151
I forgot to add "GridView1_RowCreated" to the source
0
 
LVL 13

Expert Comment

by:sm394
ID: 22810847
debug i.e put breakpoint on this line
 int age = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Age"));
and check what value you getting in age
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sort GridView by ID Descending 1 24
MailAddress in vb 4 36
Need syntax help building hyperlink 2 39
Amazon S3 Images with .Net 3 29
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question