Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change Cell Color dynamically in a Gridview Loop

Posted on 2011-03-04
9
Medium Priority
?
547 Views
Last Modified: 2012-05-11
I am trying to build a for loop that will go to each row in my gridview and check wheter a row has any of the following criterias, but I am not quite sure how to build the loop since right now I specify the column , please help
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        for (Int32 i = 0; i < GridView3.Columns.Count; i++)

        { 

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView row = e.Row.DataItem as DataRowView;


         
            if (row["NT1_Status"].ToString() == "Assigned")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.LightGreen;
            }

            else if (row["NT1_Status"].ToString() == "BR")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.Azure;
            
            }

            else if (row["NT1_Status"].ToString() == "OpenNS")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.LightSalmon;

            }
            }
        }
    }

Open in new window

0
Comment
Question by:TonyReba
[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
9 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 35040923
Ok, this event occurs for every row in your grid, thus no need for loop.
I rewrite your code, notice the way i fetch the value:
string statusValue = (string)DataBinder.Eval(e.Row.DataItem, "NT1_Status");

You need to fech any required value that way and cast it to your needs.

Cheers,
Miguel
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string statusValue = (string)DataBinder.Eval(e.Row.DataItem, "NT1_Status"); 

            if (statusValue == "Assigned")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.LightGreen;
            }

            else if (statusValue == "BR")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.Azure;

            }

            else if (statusValue == "OpenNS")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.LightSalmon;

            }
        }
    }

Open in new window

0
 
LVL 9

Author Comment

by:TonyReba
ID: 35041010
Well really is not for specific column eg. Nt1. Is any cell in the grid with those statuses.
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 35041390
I was having a similar problem and I used the following logic in the page load event instead of the row binding to get around it.

DataTable dt = (DataTable)gridView.DataSource;
int rowIndex = 0;
foreach (GridViewRow gvr in gridView.Rows)
{
gvr.DataItem = dt.Rows[rowIndex++];
DataRow dr = (DataRow)gvr.DataItem;                
object[] oa = dr.ItemArray;
if (oa[7].ToString().Length == 0)
{
gvr.Cells[7].BackColor = System.Drawing.Color.Red;
}
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Author Comment

by:TonyReba
ID: 35047489
I dont reallly see the logc there,

the function should look for all cells in the grid, but instead of saying
 string statusValue = (string)DataBinder.Eval(e.Row.DataItem, "NT1_Status");

where  "NT1_Status"  is the status field , i want to look for all cells and when the Status field of each column is for example

if (statusValue == "Assigned")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.LightGreen;
}

but NT1_Staus is just one of many databound fields which are called e.g. NT2_Status.,,,

sorrry my english is very bas hope i get the idead.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 35048283
GridView3_RowDataBound occurs every time a row is bound to data in a GridView control.
Check:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

If you want to look at all the the other values, just repeat the same construct:
string status2Value = (string)DataBinder.Eval(e.Row.DataItem, "NT2_Status");
//do similar code here
etc.
Note: For the column loop, you can even use the column name, instead of hard -coded names ("NT2_Status")

Question: Are you planning to change every cell or just cell[0] based on the contents.
0
 
LVL 9

Author Comment

by:TonyReba
ID: 35048346
I am not very familiar with GridView, but

I have a filed on the database for each column status , so the grid looks like


NT1                                        NT2                 NT3
43 Assigned                           65 BR
34                                           11 Assigned
22   BR
11

So on this case 43 and 11 cells  should change color

if (statusValue == "Assigned")
            {
                e.Row.Cells[0].BackColor = System.Drawing.Color.LightGreen;
            }
0
 
LVL 16

Assisted Solution

by:Kamal Khaleefa
Kamal Khaleefa earned 400 total points
ID: 35119999
private void GridView3_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow) {
            DataRowView x = e.Row.DataItem;
            DataTable dt = x.DataView.ToTable();


            dynamic x2 = e.Row.DataItemIndex;


            if (dt.Rows[e.Row.DataItemIndex]["NT1_Status"] == "Assigned") {
                  e.Row.BackColor = System.Drawing.Color.LightGreen;
                  e.Row.Cells[0].BackColor = System.Drawing.Color.LightGreen;
                  e.Row.Cells[1].BackColor = System.Drawing.Color.LightGreen;
                  e.Row.Cells[2].BackColor = System.Drawing.Color.LightGreen;

            } else if (dt.Rows[e.Row.DataItemIndex]["NT1_Status"] == "BR") {
                  e.Row.BackColor = System.Drawing.Color.Azure;
                  e.Row.Cells[0].BackColor = System.Drawing.Color.Azure;
                  e.Row.Cells[1].BackColor = System.Drawing.Color.Azure;
                  e.Row.Cells[2].BackColor = System.Drawing.Color.Azure;

            } else if (dt.Rows[e.Row.DataItemIndex]["NT1_Status"] == "OpenNS") {

                  e.Row.BackColor = System.Drawing.Color.LightSalmon;
                  e.Row.Cells[0].BackColor = System.Drawing.Color.LightSalmon;
                  e.Row.Cells[1].BackColor = System.Drawing.Color.LightSalmon;
                  e.Row.Cells[2].BackColor = System.Drawing.Color.LightSalmon;
            }
      }
}
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 1600 total points
ID: 35125162
Ok, your issue is that, we need to compare strings, because "Assigned" is part of the string not the string itself.
Thus my previous posted code can be modified to work with your sample data as follows:
[Snippet ID=8036243]
Note: Debug.WriteLine is optional but it will show that you are fetching the correct column names.
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                if (string.IsNullOrEmpty(GridView1.Columns[i].HeaderText))
                    continue;
                Debug.WriteLine(String.Format("Col {0}Header text {1}", i, GridView1.Columns[i].HeaderText), "GridView3_RowDataBound");
                string statusValue = (string)DataBinder.Eval(e.Row.DataItem, GridView1.Columns[i].HeaderText);

                if (statusValue.Contains("Assigned"))
                {
                    e.Row.Cells[0].BackColor = System.Drawing.Color.LightGreen;
                }

                else if (statusValue.Contains("BR"))
                {
                    e.Row.Cells[0].BackColor = System.Drawing.Color.Azure;

                }

                else if (statusValue.Contains("OpenNS"))
                {
                    e.Row.Cells[0].BackColor = System.Drawing.Color.LightSalmon;

                }
            }
        }
    }

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…

715 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