Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

gridview change cell backcolor based on variable

Posted on 2008-10-26
6
Medium Priority
?
602 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
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

886 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