Solved

GridView Cell Color Red If Text Empty?

Posted on 2011-03-03
6
772 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

16 Experts available now in Live!

Get 1:1 Help Now