Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:24:51+00:00 2026-06-04T07:24:51+00:00

I have a GridView control in an asp.net 3.5 webpage, the following code executes

  • 0

I have a GridView control in an asp.net 3.5 webpage, the following code executes in the RowDataBound Event and it changes the background color and font color, once a value in the columns: RegWaitTime, and TotalWegTime is bigger than 30,

The value comes from two computated columns in sql server which returns the result from the substraction from other two columns, the problem here is that if the values are NULL in those column, I will get and error on the code that changes the color,, sorry my english , please let me know if you need me to clarify my requirement,

Thanks in advance

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {



        // Change Wait Time Cell Rows  CHECK THE LOGIC HERE

        if (e.Row.RowType == DataControlRowType.DataRow)
         {
             // This line will get the reference to the underlying row
             DataRowView _row = (DataRowView)e.Row.DataItem;
             if (_row != null)
             {
                 // get the field value which you want to compare and
                 // convert to the corresponding data type
                 // i assume the fieldName is of int type
                 int _field = Convert.ToInt32(_row.Row["RegWaitTime"]);
                 if (_field > 30)
                 {
                     e.Row.Cells[9].BackColor = System.Drawing.Color.Red;
                     e.Row.Cells[9].Style.Add("color", "white");

                 }
                 else
                     e.Row.Cells[9].BackColor = System.Drawing.Color.Green;
                     e.Row.Cells[9].Style.Add("color", "white");
             }

         }


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // This line will get the reference to the underlying row
            DataRowView _row2 = (DataRowView)e.Row.DataItem;


            if (_row2 != null)
            {
                // get the field value which you want to compare and
                // convert to the corresponding data type
                // i assume the fieldName is of int type
                int _field = Convert.ToInt32(_row2.Row["TotalRegTime"]);
                if (_field > 30)
                {
                    e.Row.Cells[10].BackColor = System.Drawing.Color.Red;
                    e.Row.Cells[10].Style.Add("color", "white");

                }
                else
                    e.Row.Cells[10].BackColor = System.Drawing.Color.Green;
                    e.Row.Cells[10].Style.Add("color", "white");
            }
        }




     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:24:52+00:00Added an answer on June 4, 2026 at 7:24 am

    You need to change the conversion code to:

    int field=0;
    
    if(int.TryParse(_row.Row["RegWaitTime"],out field))
    {
        if(field>30)
        {
        }
    }
    

    The reason you need this is that if _row.Row["RegWaitTime"] is NULL, the actual value you’ll get is DBNull.Value which will choke with Convert.ToInt32 throwing an InvalidCastException since object cannot be cast from DBNull to other types.

    Another alternative would be:

     if (!DBNull.Value.Equals(_row.Row["RegWaitTime"]))
     {
        //then do the conversion as you are doing now. 
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On an ASP NET project I have a gridview control with the following information:
I have added a GridView control on my ASP.net webpage and data bound it
I have a GridView control in an Asp.net application, that has a <asp:buttonField> of
I have an ASP.NET page with a gridview control on it with a CommandButton
I have an ASP.NET page where I am showing products in a Gridview control.
Using asp.net 3.5 Gridview control, visual studio 2008. I have played with all the
I have an ASP.Net GridView control on an ASPX page hosted on a SharePoint
I have a gridview control in ASP.Net 2.0 and i need to export this
I have a gridview control on my asp.net page(vb.net). I also have a cancel
I have a GridView control on my ASP.NET page that binds to result set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.