?
Solved

gridview change cell backcolor based on variable

Posted on 2008-10-26
6
Medium Priority
?
601 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 2000 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
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 

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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

718 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