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. Datagridview changing the row backcolor based on value of coloum in the same grid view

Datagridview changing the row backcolor based on value of coloum in the same grid view

Scheduled Pinned Locked Moved C#
helpcss
6 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.
  • U Offline
    U Offline
    User 3950922
    wrote on last edited by
    #1

    Hi all, I have been trying to change the backcolor of specific rows depending on a condition,but when the condition is met the entire grid color changes. I already tried this and many more like this but none of them work, DataGridViewCellStyle style2 = new dataGridViewCellStyle() style2.BackColor = Color.Red; dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red; There is something which I am doing wrong. Please help me to fix this.

    P R 2 Replies Last reply
    0
    • U User 3950922

      Hi all, I have been trying to change the backcolor of specific rows depending on a condition,but when the condition is met the entire grid color changes. I already tried this and many more like this but none of them work, DataGridViewCellStyle style2 = new dataGridViewCellStyle() style2.BackColor = Color.Red; dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red; There is something which I am doing wrong. Please help me to fix this.

      P Offline
      P Offline
      Pedram Behroozi
      wrote on last edited by
      #2

      dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;
      dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Red;

      And your index can change depending on your condition. [Edit] And if you want to change multiple rows BackColor:

      DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows;
      foreach (DataGridViewRow row in coll)
      row.DefaultCellStyle.BackColor = Color.Red;

      [/Edit]

      When you're alone in the Dark, Fear will protect you...

      modified on Sunday, November 2, 2008 8:41 AM

      U 1 Reply Last reply
      0
      • P Pedram Behroozi

        dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;
        dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Red;

        And your index can change depending on your condition. [Edit] And if you want to change multiple rows BackColor:

        DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows;
        foreach (DataGridViewRow row in coll)
        row.DefaultCellStyle.BackColor = Color.Red;

        [/Edit]

        When you're alone in the Dark, Fear will protect you...

        modified on Sunday, November 2, 2008 8:41 AM

        U Offline
        U Offline
        User 3950922
        wrote on last edited by
        #3

        Hi Pedram , I tried this as well it does not work, first I selected all rows which meet the specified condition This is just to show the selection, Its alternate rows here but in real scenario its any random row. int yyy = 0; foreach (DataGridViewRow rowss in dataGridView1.Rows) { if (yyy % 2 == 0) { rowss.Selected = true; } else { } yyy++; } , then I used your or each statement but still I get the same results. There is no color on the datagridview. This Datagridview is placed on a user Object and It is added on a form at the run time.Does it make any difference ? Regards Omer

        P 1 Reply Last reply
        0
        • U User 3950922

          Hi Pedram , I tried this as well it does not work, first I selected all rows which meet the specified condition This is just to show the selection, Its alternate rows here but in real scenario its any random row. int yyy = 0; foreach (DataGridViewRow rowss in dataGridView1.Rows) { if (yyy % 2 == 0) { rowss.Selected = true; } else { } yyy++; } , then I used your or each statement but still I get the same results. There is no color on the datagridview. This Datagridview is placed on a user Object and It is added on a form at the run time.Does it make any difference ? Regards Omer

          P Offline
          P Offline
          Pedram Behroozi
          wrote on last edited by
          #4

          But following completely worked for me:

          int y = 0;
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
          if (y % 2 == 0)
          row.Selected = true;
          y++;
          }

          DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows;
          foreach (DataGridViewRow row in coll)
              row.DefaultCellStyle.BackColor = Color.Red;
          

          But why did you act like that? It's O(n) = 2n You can:

          int y = 0;
          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
          if (y % 2 == 0)
          row.DefaultCellStyle.BackColor = Color.Red;
          y++;
          }

          DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows;
          foreach (DataGridViewRow row in coll)
              row.DefaultCellStyle.BackColor = Color.Red;
          

          Member 3953856 wrote:

          .Does it make any difference ?

          I don't think so. You should just point to your DataGridView correctly.

          When you're alone in the Dark, Fear will protect you...

          1 Reply Last reply
          0
          • U User 3950922

            Hi all, I have been trying to change the backcolor of specific rows depending on a condition,but when the condition is met the entire grid color changes. I already tried this and many more like this but none of them work, DataGridViewCellStyle style2 = new dataGridViewCellStyle() style2.BackColor = Color.Red; dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red; There is something which I am doing wrong. Please help me to fix this.

            R Offline
            R Offline
            richardw48
            wrote on last edited by
            #5

            Put your code into the cellformat event of the datagridview

            U 1 Reply Last reply
            0
            • R richardw48

              Put your code into the cellformat event of the datagridview

              U Offline
              U Offline
              User 3950922
              wrote on last edited by
              #6

              Yes , putting the code on the cell formatting event of the datagridview worked, I mentioned that I was using a user control. The data grid was on the User control. If not a user control then the other stuff works fine. thanks

              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