DataGridView rows cannot change background color during initialization

I have the following problem: TabControl has three tabs. Each TabPage has its own DataGridView. In the "Enter" event, some lines change the background color. When the form starts to initialize, a function is called that changes color. But the DataGridView rows have their own background by default (the background color is not changed). If I click on another tab and return to the first tab, the function will be called again and the background will be changed. So, why doesn’t this happen the first time at the initialization stage (the function is called, but the lines do not change color). How to make a DataGridView change the background color of its rows at the initialization stage? Many thanks!

+3
source share
3 answers

How do you change color? You might want to look at the CellFormatting event to see if you can explicitly draw a cell when it is visible to the user. Thus, every time he redraws a cell, you can guarantee that the color will be correct. (Assuming you need different colors for the rows, otherwise just set the cell style and nullify the control.

UPDATE:

It is important that you refer to the cellstyle through args of events, otherwise you will do recursion to work out the style of the cell you are trying to access.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    e.CellStyle.BackColor = Color.PaleGreen
}
+1
source

DataGridViewCellStyle. , TabControl, , , cellstyles, , DataGridView, DataGridView TabControl SelectedIndexChanged .

+1

, , , . , . DGV , , , , :

this.tabControl1.SelectedTab = tabPage2;
0

Source: https://habr.com/ru/post/1761712/


All Articles