Solved

GridView Cell Color Red If Text Empty?

Posted on 2011-03-03
6
775 Views
Last Modified: 2012-05-11
The following code changes the entire column to Red, however I would like to just change Only cells with empty text.

Q. How can I change Only certain Cells Red and not the entire column?

    protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if(e.Row.Cells[7].Text.Contains(""))
                e.Row.Cells[7].BackColor = System.Drawing.Color.Red;
        }
    }
0
Comment
Question by:pointeman
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:himanshut
ID: 35032861
maybe coz your that column would be empty that's why the condition is true to get the cell color Red
0
 
LVL 11

Accepted Solution

by:
MajorBigDeal earned 500 total points
ID: 35033337
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
 

Author Comment

by:pointeman
ID: 35037079
Okay, here's how it's done in RowDataBound (similar to MajorBigDeal).

I can now read text of both:
asp:BoundField
asp:TemplateField (Text='<%# Bind("FirstName") %>'>).


    protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            foreach (GridViewRow row in gridView1.Rows)
            {                              
                if(row.Cells[6].Text == "10")
                row.Cells[6].BackColor = System.Drawing.Color.Red;

                if(((Label)row.FindControl("label1")).Text == "Jim")
                    ((Label)row.FindControl("label1")).BackColor = System.Drawing.Color.Green; //right-next to cell[6].

            }    
    }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pointeman
ID: 35037372
New Problem:
  1. asp:CommandField won't 'fire', throws error:
        "Object reference not set to an instance of an object."
   
However, asp:ButtonField works fine.
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 35041069
Yes but it doesn't make sense to put that in row data bound because you are iterating the entire grid for every row. For example, if your grid has 100 rows , your loop will run 10000 times.  I guess it doesn;t really matter but I think it would be better in the page load.  

I want to help with your new problem, but there isn't enough info,  Also, if you open a new question for that, you'll probably get more responses since experts are always jumping on new questions.
0
 

Author Comment

by:pointeman
ID: 35059938
I'll give you points because your solution works, but not in RowDataBound.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
A simple description of email encryption using a secure portal service. This is one of the choices offered by The Email Laundry for email encryption. The other choices are pdf encryption which creates an encrypted pdf of your email and any attachmen…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now