Solved

GridView Cell Color Red If Text Empty?

Posted on 2011-03-03
6
779 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

791 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