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


GridView Cell Color Red If Text Empty?

Posted on 2011-03-03
Medium Priority
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)
                e.Row.Cells[7].BackColor = System.Drawing.Color.Red;
Question by:pointeman
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
  • 3
  • 2
LVL 10

Expert Comment

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

Accepted Solution

MajorBigDeal earned 2000 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;

Author Comment

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

I can now read text of both:
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].

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.


Author Comment

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.
LVL 11

Expert Comment

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.

Author Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

705 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