Hello everybody! I have the next problem: TabControl has three TabPages. Every TabPage has its own DataGridView. On "Enter" event some rows change their background color. When the form begin to initialize, the function, that changes color, is called. But DataGridView rows have their default background (background color hasn't been changed). If I click on another TabPage and then come back to the first TabPage the function is called again and the background is changed. So, why it doesn't happen at first time on the initialization stage (the function is called, but the rows don't change their color). How can I force the DataGridView to change the background color of its rows at the initialization stage? Thanks a lot!
views:
29answers:
1
A:
How are you changing the colour? You might want to look into the CellFormatting event to see whether you can explicitly paint the cell when it is visible to the user. That way each time it redraws the cell you can guarantee the colour will be correct. (Assuming you need different colours for the rows, otherwise just set the cell style and invalidate the control.
UPDATE:
It's important that you reference the cellstyle via the event args, otherwise you will do recursion to work out the cell style that you are trying to access.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.CellStyle.BackColor = Color.PaleGreen
}
Spence
2010-08-26 11:53:04
This function change backgroundforeach (DataGridViewRow row in gridView.Rows) { row.DefaultCellStyle.BackColor = Color.PaleGreen; }I try call my function into CellFormatting. After your advice the requirement of the resources increased and the speed of working was sharply down. How can i improve the situation and increase the perfomance or can you advice another method to solve the previous problem?
Deniplane
2010-08-26 13:09:21
I've updated my answer. My advice is to ensure that you don't do recursive work inside the CellFormatting.
Spence
2010-08-26 13:28:29
Thank you, Spence for your help. The reason, really, was in the implicit recursion. But to change the color of every cell is too costly. I use CellFormatting event but check the condition to change the color of the whole row. As result the function works quickly and correctly. Thanks a lot!
Deniplane
2010-08-26 15:29:32