After getting a request to make the ERP program I wrote change a DataGridView row's background color based on values in the row, I thought I would post it here as a tip in case any of you ever had to do this.
There are a few ways of accomplishing this, but the best way I found was to use the DataGridView's RowPrePaint event. It does it's job nicely whenever the rows need to be redrawn.
This code assumes that you have a DataGridView named MyDataGridView, and a checkbox column named "IsColored".
Create an event handler for the RowPrePaint event, and put your code in there.
private void MyDataGridView_RowPrePaint(object sender,
MyDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
What the above code does, is prior to painting any row, it checks to see if the "IsColored" column value is true. If so, it sets the background color of all of the cells in the row to the color red.
You can adjust this to check for any value in any column and act accordingly. For instance if you had a column that contained a salary or something, you could change the if statement to read "if (MyDataGridView["Salary"] > 50000)". This would cause the row to highlight on anyone making over 50K. You can also do this based on the values of multiple columns, column values in another DataGridView, or the state or value of any other control on the form.
Well that's my tip for today. I hope someone finds it useful.