简体   繁体   中英

Rows in DataGridView are not changing color

I am just trying to put the color red for the back color of the rows that have the value of 4 in a specific column but all the rows have white back color even though there are rows with value more than 4. I also stepped through the code so I know that the code actually execute the code to change the backcolor.

What am I doing wrong here.

dataGridViewMain.DataSource = table;
dataGridViewMain.Sort(dataGridViewMain.Columns["Days in the shop"], ListSortDirection.Descending);
      foreach (DataGridViewRow row in this.dataGridViewMain.Rows)
      {
        if (Convert.ToInt32(row.Cells["Days in the shop"].Value) > 4)
           row.DefaultCellStyle.BackColor = Color.Red;
      }

Apparently I need to put the logic inside RowPostPaint event. It's all good now.

I have had problems with DataGridView cell colors when creating child forms, it was hard for me to find a solution. If using a child form I had to make sure I changed the color from the Form Load Event. I was initially trying to make changes to color from the main method, which did not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM