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. datagrid row color

datagrid row color

Scheduled Pinned Locked Moved C#
visual-studiohelptutorial
9 Posts 4 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.
  • E Offline
    E Offline
    Erdinc27
    wrote on last edited by
    #1

    hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color

    M Q D 3 Replies Last reply
    0
    • E Erdinc27

      hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Use the oncellpaint event (I think) and query the object to find what row/column is being painted and set the background accordingly. Note that this fires for each cell that is on display, not the entire data source

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • E Erdinc27

        hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color

        Q Offline
        Q Offline
        Qendro
        wrote on last edited by
        #3

        did you try this:

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
        dataGridView1.BackgroundColor = Color.Red;
        if (i % 2 == 0)
        {
        dataGridView1.BackgroundColor = Color.Blue;
        }
        }

        Qendro

        D 1 Reply Last reply
        0
        • Q Qendro

          did you try this:

          for (int i = 0; i < dataGridView1.Rows.Count; i++)
          {
          dataGridView1.BackgroundColor = Color.Red;
          if (i % 2 == 0)
          {
          dataGridView1.BackgroundColor = Color.Blue;
          }
          }

          Qendro

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          Surely that just sets the background colour for the whole DataGridView, not for individual rows?

          Q 1 Reply Last reply
          0
          • E Erdinc27

            hi all i have a small problem..actually i searched in the forum but i couldnt see the same problem with mine..so here is my problem..i want to have two different color in my datagridview...for example first row will be blue and second one white and third one again blue and 4th one will be white and so on..i used if block and % in it but i couldnt find anything in the intellisense about datagridview color

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            You can set the cell style to use on alternate Rows within the DataGridView. This article shows how to do it: http://www.dotnetcurry.com/ShowArticle.aspx?ID=132&AspxAutoDetectCookieSupport=1[^] Tip 12.

            E 1 Reply Last reply
            0
            • D David Skelly

              Surely that just sets the background colour for the whole DataGridView, not for individual rows?

              Q Offline
              Q Offline
              Qendro
              wrote on last edited by
              #6

              i tried this and it worked:

              private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
              {
              for (int i = 0; i < dataGridView1.Columns.Count; i++)
              {
              e.CellStyle.BackColor = Color.Red;
              if (i % 2 == 0)
              {
              e.CellStyle.BackColor = Color.Blue;
              }
              }
              }

              Qendro

              D E 2 Replies Last reply
              0
              • Q Qendro

                i tried this and it worked:

                private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
                {
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                e.CellStyle.BackColor = Color.Red;
                if (i % 2 == 0)
                {
                e.CellStyle.BackColor = Color.Blue;
                }
                }
                }

                Qendro

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                It might work (if you replace Columns with Rows), but it doesn't look very efficient. Also, you don't need to do this anyway since DataGridView has a built in property that does it for you (AlternatingRowsDefaultCellStyle).

                1 Reply Last reply
                0
                • Q Qendro

                  i tried this and it worked:

                  private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
                  {
                  for (int i = 0; i < dataGridView1.Columns.Count; i++)
                  {
                  e.CellStyle.BackColor = Color.Red;
                  if (i % 2 == 0)
                  {
                  e.CellStyle.BackColor = Color.Blue;
                  }
                  }
                  }

                  Qendro

                  E Offline
                  E Offline
                  Erdinc27
                  wrote on last edited by
                  #8

                  hi guys thanks for the reply i tried qendro's codes but i have a small problem with it also..it works but not as i wished..it works like that : if i have one or 3 rows in datagridview then datagridview's backcolor becomes blue but when i add another row then it turns to white.. u see it is a bit different than i wished.. i want like that first row will be blue and second white and third blue again and fourth will be white again and so on..any other suggestions you have thanks all for the replies

                  1 Reply Last reply
                  0
                  • D David Skelly

                    You can set the cell style to use on alternate Rows within the DataGridView. This article shows how to do it: http://www.dotnetcurry.com/ShowArticle.aspx?ID=132&AspxAutoDetectCookieSupport=1[^] Tip 12.

                    E Offline
                    E Offline
                    Erdinc27
                    wrote on last edited by
                    #9

                    yeah man u are right it works now as i wanted thanks all u for your help

                    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