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. Cange datagridview cel backcolor if cel has value

Cange datagridview cel backcolor if cel has value

Scheduled Pinned Locked Moved C#
question
5 Posts 3 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.
  • G Offline
    G Offline
    GrooverFromHolland
    wrote on last edited by
    #1

    Hi, I use this to change the back color of a cell:

    private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
    if (!string.IsNullOrEmpty(e.Value.ToString()))
    {
    e.CellStyle.BackColor = Color.LightGoldenrodYellow;
    }
    }

    This works, but also check box cells are changed if they are checked or not. How can I make an exception for check boxes, or is there an other way to do this? The datagridview is set to autogenerateColumns = true. Groover,

    0200 A9 23 0202 8D 01 80 0205 00

    W A 2 Replies Last reply
    0
    • G GrooverFromHolland

      Hi, I use this to change the back color of a cell:

      private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
      {
      if (!string.IsNullOrEmpty(e.Value.ToString()))
      {
      e.CellStyle.BackColor = Color.LightGoldenrodYellow;
      }
      }

      This works, but also check box cells are changed if they are checked or not. How can I make an exception for check boxes, or is there an other way to do this? The datagridview is set to autogenerateColumns = true. Groover,

      0200 A9 23 0202 8D 01 80 0205 00

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      One way to distinguish columns is to check the ColumnIndex property of the DataGridViewCellFormattingEventArgs object. For example you can access the actual column being formatted using

      dataGridViewData.Columns[e.ColumnIndex]

      For more info, refer to DataGridViewCellFormattingEventArgs[^]

      G 1 Reply Last reply
      0
      • W Wendelius

        One way to distinguish columns is to check the ColumnIndex property of the DataGridViewCellFormattingEventArgs object. For example you can access the actual column being formatted using

        dataGridViewData.Columns[e.ColumnIndex]

        For more info, refer to DataGridViewCellFormattingEventArgs[^]

        G Offline
        G Offline
        GrooverFromHolland
        wrote on last edited by
        #3

        Hi, I've already been there, but I wanted something simple. Have it working now, this is what I use:

        private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
        int x =e.ColumnIndex;
        int y = e.RowIndex;

               if (dataGridViewZakdata\[x,y\].GetType() == typeof(DataGridViewTextBoxCell))
               {
                   if (!string.IsNullOrEmpty(e.Value.ToString()))
                   {
                       e.CellStyle.BackColor = Color.LightGoldenrodYellow;
                   }
               }
           }
        

        Thank You for your efford, Groover,

        0200 A9 23 0202 8D 01 80 0205 00

        1 Reply Last reply
        0
        • G GrooverFromHolland

          Hi, I use this to change the back color of a cell:

          private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
          {
          if (!string.IsNullOrEmpty(e.Value.ToString()))
          {
          e.CellStyle.BackColor = Color.LightGoldenrodYellow;
          }
          }

          This works, but also check box cells are changed if they are checked or not. How can I make an exception for check boxes, or is there an other way to do this? The datagridview is set to autogenerateColumns = true. Groover,

          0200 A9 23 0202 8D 01 80 0205 00

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          You could test the DataGridViewCellFormattingEventArgs.DesiredType property. It will be typeof(string) for textbox cells and typeof(bool) for checkbox cells. Alan.

          G 1 Reply Last reply
          0
          • A Alan N

            You could test the DataGridViewCellFormattingEventArgs.DesiredType property. It will be typeof(string) for textbox cells and typeof(bool) for checkbox cells. Alan.

            G Offline
            G Offline
            GrooverFromHolland
            wrote on last edited by
            #5

            Thanks Alan, Now I have it for both types in two lines:

            private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
            if(e.DesiredType==typeof(string) && (!string.IsNullOrEmpty(e.Value.ToString())))
            e.CellStyle.BackColor = Color.LightGoldenrodYellow;

                   if(e.DesiredType==typeof(bool)&&((Convert.ToBoolean(e.Value) == true)))
                      e.CellStyle.BackColor = Color.LightGoldenrodYellow;
               }
            

            Edited: I did not test it thorough, if I insert a new row in database I get an exception:

            if(e.DesiredType==typeof(bool)&&((Convert.ToBoolean(e.Value) == true)))

            Object cannot be cast from DBNull to other types. Groover,

            0200 A9 23 0202 8D 01 80 0205 00

            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