Highlight Gridview when Column is NULL

I have a simple gridview that I want to highlight a row if the 12th column is null. This is what I have now in the RowDataBound event:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[12].Text == "" || e.Row.Cells[12].Equals(DBNull.Value))
                {
                    e.Row.BackColor = System.Drawing.Color.Yellow;
                }
            }
        }

Open in new window

In the database the int field is nullable. I can't figure out why the rows are not highlighting.
Carla RomereDirector of Information TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guru_samiCommented:
Try this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
     DataRowView drv = e.Row.DataItem as DataRowView;
     if (drv["YourFieldName"]==null)
     {
             e.Row.BackColor = System.Drawing.Color.Yellow;
     }
}

Open in new window

0
Carla RomereDirector of Information TechnologyAuthor Commented:
Still not highlighting the rows.
0
guru_samiCommented:
Set the breakpoints on line 4 and 6
-> See what is the value of drv["YourFieldName"]
-> Does your code hit the line 6?
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Carla RomereDirector of Information TechnologyAuthor Commented:
Okay that was just weird. I put a breakpoint on the two lines and ran Debug. It stopped on the first line and just sat there acting like it was doing something, but it never got past it. I got tired of waiting and stopped debugging and then the data showed up, but still was not highlighted.
0
guru_samiCommented:
Add this line:
 DataRowView drv = e.Row.DataItem as DataRowView;
Response.Write( "My Field Value " +drv["YourFieldName"].ToString());
     if (drv["YourFieldName"]==null)
     {
             e.Row.BackColor = System.Drawing.Color.Yellow;
     }
0
Carla RomereDirector of Information TechnologyAuthor Commented:
I get this (for every row in the table, around 400).
My Field Value My Field Value 156627

The ones with a value in the field are returning. I'm going to try running it as an empty string instead of null and see what happens.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
Empty string didn't do anything different than the null ones. The data pulls correctly, the Response.Write sees the data, just no highlighted lines.
0
guru_samiCommented:
Why would you return an int as empty string?
Put a check for empty string as well like:
if (drv["YourFieldName"]==null ||(String.InNullOrEmpty(drv["YourFieldName"].ToString()))
     {
             e.Row.BackColor = System.Drawing.Color.Yellow;
     } 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carla RomereDirector of Information TechnologyAuthor Commented:
That did it. I was trying for the empty string just to see if it was coming across as a string. But the last piece you posted did the trick. I've not used IsNullOrEmpty before and and I will be using it more now!
0
Carla RomereDirector of Information TechnologyAuthor Commented:
Final working code:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = e.Row.DataItem as DataRowView;
                if (String.IsNullOrEmpty(drv["GRN_NUMBER"].ToString()))
                {
                    e.Row.BackColor = System.Drawing.Color.Yellow;
                }
            }
        }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.