Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trying to make a DataGridView field a color if a condition is met.

Trying to make a DataGridView field a color if a condition is met.

Scheduled Pinned Locked Moved C#
help
18 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Norris Chappell

    sorry I meant 0.9500

    M Offline
    M Offline
    Mathi Mani
    wrote on last edited by
    #8

    decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
    if (ActualFTE > 1.25)

    In the above code check whether e.Row.Cells[5].Text returns data that can be converted to decimal. Or add ToString() at the end and see if it helps.

    decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text.ToString());

    N 1 Reply Last reply
    0
    • M Mathi Mani

      decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text);
      if (ActualFTE > 1.25)

      In the above code check whether e.Row.Cells[5].Text returns data that can be converted to decimal. Or add ToString() at the end and see if it helps.

      decimal ActualFTE=Convert.ToDecimal(e.Row.Cells[5].Text.ToString());

      N Offline
      N Offline
      Norris Chappell
      wrote on last edited by
      #9

      Same error: Input string was not in a correct format.

      M 1 Reply Last reply
      0
      • N Norris Chappell

        Same error: Input string was not in a correct format.

        M Offline
        M Offline
        Mathi Mani
        wrote on last edited by
        #10

        Just to make sure everything is fine, try hardcoding the value and see if you still get the error.

        decimal ActualFTE=Convert.ToDecimal("0.9500");

        If this works without exception, then you can be sure, the problem is with the expression e.Row.Cells[5].Text. This expression you use should return some string (like your value 0.9500) that can be converted to decimal for the conversion to work without exception.

        N 1 Reply Last reply
        0
        • M Mathi Mani

          Just to make sure everything is fine, try hardcoding the value and see if you still get the error.

          decimal ActualFTE=Convert.ToDecimal("0.9500");

          If this works without exception, then you can be sure, the problem is with the expression e.Row.Cells[5].Text. This expression you use should return some string (like your value 0.9500) that can be converted to decimal for the conversion to work without exception.

          N Offline
          N Offline
          Norris Chappell
          wrote on last edited by
          #11

          okay it didn't error this time but nothing changed.

          M 1 Reply Last reply
          0
          • N Norris Chappell

            okay it didn't error this time but nothing changed.

            M Offline
            M Offline
            Mathi Mani
            wrote on last edited by
            #12

            May be the value ActualFTE is not greater than 1.25, as per your condition.

               if (ActualFTE > 1.25)
               {
                  e.Row.BackColor = Color.Red;
                }
            

            So the color will not be changed.

            N 2 Replies Last reply
            0
            • M Mathi Mani

              May be the value ActualFTE is not greater than 1.25, as per your condition.

                 if (ActualFTE > 1.25)
                 {
                    e.Row.BackColor = Color.Red;
                  }
              

              So the color will not be changed.

              N Offline
              N Offline
              Norris Chappell
              wrote on last edited by
              #13

              That is true for most of the 251 rows. I only see a couple that are over 1.25. So I should take out that code?

              1 Reply Last reply
              0
              • M Mathi Mani

                May be the value ActualFTE is not greater than 1.25, as per your condition.

                   if (ActualFTE > 1.25)
                   {
                      e.Row.BackColor = Color.Red;
                    }
                

                So the color will not be changed.

                N Offline
                N Offline
                Norris Chappell
                wrote on last edited by
                #14

                Do you think I should post all of my code to see if I have something missing?

                M 1 Reply Last reply
                0
                • N Norris Chappell

                  Do you think I should post all of my code to see if I have something missing?

                  M Offline
                  M Offline
                  Mathi Mani
                  wrote on last edited by
                  #15

                  When you want the row to be colored? Change the if condition based on that. It should work.

                  N 1 Reply Last reply
                  0
                  • M Mathi Mani

                    When you want the row to be colored? Change the if condition based on that. It should work.

                    N Offline
                    N Offline
                    Norris Chappell
                    wrote on last edited by
                    #16

                    Not the whole row only that field. ActualFTE.

                    using System;
                    using System.Configuration;
                    using System.Data;
                    using System.Data.SqlClient;
                    using System.Web.UI;
                    using System.Drawing;
                    using System.Web.UI.WebControls;
                    using System.Web.UI.WebControls.WebParts;

                    namespace StaffingWebParts.CatwComment
                    {
                    public partial class CatwCommentUserControl : UserControl
                    {
                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
                    SqlCommand cmd = new SqlCommand();

                        protected void Page\_Load(object sender, EventArgs e)
                        {
                            lblMsg.Text = "";
                            if (!Page.IsPostBack)
                            {
                                BindSubjectData();
                            }
                        }
                        protected void highlightrow(object sender, GridViewRowEventArgs e)
                        {
                           if (e.Row.RowType == DataControlRowType.DataRow)
                           {
                             decimal ActualFTE = Convert.ToDecimal(e.Row.Cells\[5\].Text.ToString());
                               
                             if (ActualFTE > 1.2500M)
                               {
                                  e.Row.BackColor = Color.Red;
                                }
                            }
                        }
                        //call to bind gridview
                        protected void BindSubjectData()
                        {
                            using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString))
                            {
                                using (SqlCommand cmd = new SqlCommand())
                                {
                                    cmd.CommandText = ("select a.id, b.StaffTrackingID, a.ResourceName, b. EstimateHours, EstimateFTE, b.ActualHours, b.ActualFTE,b.Comment, b.CommentBy, b.Period  from  StaffTracking a, StaffTrackingFTEData b where a.id = b.StaffTrackingid  order by ResourceName");
                                    cmd.Connection = sqlCon;
                                    sqlCon.Open();
                                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                                    DataTable dt = new DataTable();
                                                   
                                    da.Fill(dt);
                                    if (dt.Rows.Count > 0)
                                    {
                                        gvCATW.DataSource = dt;
                                        gvCATW.DataBind();
                                    }
                                    else
                                    {
                                        DataRow dr = dt.NewRow();
                                        dt.Rows.Add(dr);
                                        gvCATW.DataSource = dt;
                                        gvCATW.DataBind();
                                        gvCATW.Rows\[0\].Visible = false;
                    
                    M 1 Reply Last reply
                    0
                    • N Norris Chappell

                      Not the whole row only that field. ActualFTE.

                      using System;
                      using System.Configuration;
                      using System.Data;
                      using System.Data.SqlClient;
                      using System.Web.UI;
                      using System.Drawing;
                      using System.Web.UI.WebControls;
                      using System.Web.UI.WebControls.WebParts;

                      namespace StaffingWebParts.CatwComment
                      {
                      public partial class CatwCommentUserControl : UserControl
                      {
                      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
                      SqlCommand cmd = new SqlCommand();

                          protected void Page\_Load(object sender, EventArgs e)
                          {
                              lblMsg.Text = "";
                              if (!Page.IsPostBack)
                              {
                                  BindSubjectData();
                              }
                          }
                          protected void highlightrow(object sender, GridViewRowEventArgs e)
                          {
                             if (e.Row.RowType == DataControlRowType.DataRow)
                             {
                               decimal ActualFTE = Convert.ToDecimal(e.Row.Cells\[5\].Text.ToString());
                                 
                               if (ActualFTE > 1.2500M)
                                 {
                                    e.Row.BackColor = Color.Red;
                                  }
                              }
                          }
                          //call to bind gridview
                          protected void BindSubjectData()
                          {
                              using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString))
                              {
                                  using (SqlCommand cmd = new SqlCommand())
                                  {
                                      cmd.CommandText = ("select a.id, b.StaffTrackingID, a.ResourceName, b. EstimateHours, EstimateFTE, b.ActualHours, b.ActualFTE,b.Comment, b.CommentBy, b.Period  from  StaffTracking a, StaffTrackingFTEData b where a.id = b.StaffTrackingid  order by ResourceName");
                                      cmd.Connection = sqlCon;
                                      sqlCon.Open();
                                      SqlDataAdapter da = new SqlDataAdapter(cmd);
                                      DataTable dt = new DataTable();
                                                     
                                      da.Fill(dt);
                                      if (dt.Rows.Count > 0)
                                      {
                                          gvCATW.DataSource = dt;
                                          gvCATW.DataBind();
                                      }
                                      else
                                      {
                                          DataRow dr = dt.NewRow();
                                          dt.Rows.Add(dr);
                                          gvCATW.DataSource = dt;
                                          gvCATW.DataBind();
                                          gvCATW.Rows\[0\].Visible = false;
                      
                      M Offline
                      M Offline
                      Mathi Mani
                      wrote on last edited by
                      #17

                      Refere this question for changing the color of a grid row based on some condition. The code is in Vb.Net but you can get the idea. How Can I Change A Rows Backcolour Based On The Value In The First Cell Of The Same Row[^]

                      N 1 Reply Last reply
                      0
                      • M Mathi Mani

                        Refere this question for changing the color of a grid row based on some condition. The code is in Vb.Net but you can get the idea. How Can I Change A Rows Backcolour Based On The Value In The First Cell Of The Same Row[^]

                        N Offline
                        N Offline
                        Norris Chappell
                        wrote on last edited by
                        #18

                        Mathi, I was able to get it to work. Thank you for your help and patience. You and others have help me to complete this project.

                        protected void gvCATW_RowDataBound(object sender, GridViewRowEventArgs e)

                        {
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {

                        // To check condition on integer value
                        if (Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ActualFTE")) > 1.25M)
                        {
                          e.Row.BackColor = System.Drawing.Color.Red;
                        }
                        

                        }

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups