Solved

GridView BackColor

Posted on 2014-09-30
4
214 Views
Last Modified: 2014-09-30
I have a gridview that looks like this. There a way I can extend the backcolor of the row so that goes to multiple rows. For example, show the color blue from 1.5 to 3.25 for Dan, the color red from 1.5 to 2.75 for Mike
        Dan     Mike        John
1                                      X
1.5       X      X

2
2.5
2.75             X
3
3.25      X
4
5                             X
 
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
                        DataRowView drview = e.Row.DataItem as DataRowView;
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                            if (e.Row.Cells[1].Text == "1")
                            {
                                e.Row.Cells[1].Text = "X";
                                e.Row.Cells[1].BackColor = System.Drawing.Color.Blue;
                            }
                            if (e.Row.Cells[2].Text == "1")
                            {
                                e.Row.Cells[2].Text = "X";
                                e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
                            }
                            if (e.Row.Cells[3].Text == "1")
                            {
                                e.Row.Cells[3].Text = "X";
                                e.Row.Cells[3].BackColor = System.Drawing.Color.Yellow;
                            }
                        }
        }

Open in new window

0
Comment
Question by:RecipeDan
[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
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 40353717
I think the RowDataBound event is not the right place for setting the colours for the continuous rows. You need to set it in a function after you have build the whole grid. The code will be as simple as this:
Color clr[3] = {System.Drawing.Color.Blue, System.Drawing.Color.Red, System.Drawing.Color.Yellow}; // add more colours here
for(int i = 0; i < min(GridView1.Columns.Count, 3); i++)
{
  bool started = false;
  foreach(GridViewRow row in GridView1.Rows)
  {
    if(row.Cells[i].Text == "X")
    {
      if(!started) started = true; else break;
    }
    if(started) row.Cells[i].BackColor = clr[i];
  }
}

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40353835
I am getting two errors with your code:

bad array declarator
min does not exist in the current context

I fixed the errors and loaded LoadColorsGrid from RowDataBound after everything was done. Should I have loaded it from somewhere else? I attached a screenshot of the results but it is not correct neither. I do not know why one column has two rows colored and the second column only has one and the third has none.
        protected void LoadColorsGrid()
        {
            System.Drawing.Color[] colorList = new System.Drawing.Color[3];             
            colorList[0] = Color.Blue;            
            colorList[1] = Color.Red;            
            colorList[2] = Color.Yellow; 
 
            for(int i = 0; i < 3 ; i++)
            {
              bool started = false;
              foreach(GridViewRow row in GridView1.Rows)
              {
                if(row.Cells[i].Text == "X")
                {
                  if(!started) started = true; else break;
                }
                if(started) row.Cells[i].BackColor = colorList[i];
              }
            }
        }

Open in new window

Screenshot.png
0
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 40353845
Thanks for testing. I figured the errors. This code will do the job:
        protected void LoadColorsGrid()
        {
            System.Drawing.Color[] colorList = new System.Drawing.Color[3];             
            colorList[0] = Color.Blue;            
            colorList[1] = Color.Red;            
            colorList[2] = Color.Yellow; 
 
            for(int i = 0; i < 3 ; i++)
            {
              bool started = false;
              foreach(GridViewRow row in GridView1.Rows)
              {
                if(row.Cells[i+1].Text == "X")
                {
                  row.Cells[i+1].BackColor = colorList[i];
                  if(!started) started = true; else break;
                }
                if(started) row.Cells[i+1].BackColor = colorList[i];
              }
            }
        }

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40353862
It works great. Thank you for your assistance. I spent the last two days trying to figure it out.
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
Save json data from URL using SSIS 1 27
Visual Studio npm 1 29
How to repeat the data 4 30
Selenium and Xpath 4 31
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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