When is Datagridview's Paint and Cellpainting called?

1.7k views Asked by At

I have a Datagridview dgv, let's say with 3 of the columns I want to affect manually are :

col1 : DataGridviewTextboxColumn, for displaying a number

col2 : DataGridviewTextboxColumn, used as a color indicator, without text. BackColor will will be changed later.

col3 : DataGridviewImageColumn

Other columns are binded with their respective DataPropertyName of the underlying Datasource. The 3 columns above are not binded to any DataPropertyName and to be affected manually, for example when I want to change the image or color indicator.

Every time I affect the DataSource, I do a datasource rebind and dgv.Invalidate() to update the grid's graphic.

  1. When and how do I ask the dgv to take into account the manual values in 3 edited columns ? Previously I manually affect the 3 above column inside dgv_CellPainting. It will work but CellPainting takes a long time and we decide to remove it. I tried putting them inside dgv_Paint (which I used for merging headers) but it does not take the color nor the image.

With CellPainting : rectangles for spaces between rows, filling the background, separater between rows, boxes for image and color, etc. The 3 columns I am refering to are the first column (index), color and image (in boxes) columns. Other image are constant.

enter image description here

Without CellPainting: No row and column separation. I created a pseudo row separation by setting grid's cell border to horizontal.

enter image description here

  1. When is CellPainting and Paint called? What is the execution order and their difference? My rough understanding is that they are called when the cell/grid "need to be repainted". But I notice they are called multiple times, not just when I call Invalidate() but even when I hover the mouse over the grid region.

Thanks all.

0

There are 0 answers