0

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.

Kenny
  • 1,902
  • 6
  • 32
  • 61
  • Yes, you got that right, it is called quie often. If you have a perfomance problem you may want to try limiting the actions to certain rows&columns. what/how much are you doing in the cellpainting event? maybe it can be optimized? eg: calling the built-in methods as much as possible will yield in a better performance instead of using drawstring etc all the time.. – TaW Jun 10 '15 at 14:24
  • Yes I did try to limit to the max the cellpainting, only create rectangles (so that rows do not touch each other), fill those with colored pen (following MDSN's https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting%28v=vs.110%29.aspx ), the rest I call e.PaintContent(e.CellBounds);to avoid DrawString, DrawImage, etc. Even then it is slow. – Kenny Jun 10 '15 at 14:26
  • @TaW: What do you mean by "if that is all"? Then if I want to affect the manually-edited columns, when should it be done? – Kenny Jun 10 '15 at 14:31
  • Sorry I caught an early version of your comment, containing only e.PaintContent(e.CellBounds);`` - Now things look different..! Hm, how many rows and cols are there? – TaW Jun 10 '15 at 14:34
  • 9 columns: 4 image columns and 5 text columns. The lagginess begins to show at 4-5th rows. Basically when I resize the winform TaskPane horizontally (it's a winform taskpane application for Excel) the grid takes time to be fully painted. – Kenny Jun 10 '15 at 14:42
  • The numbers sond like nothing. otoh: imagecolumns could be more than that. do you (have to) draw these in the cellpainting event? – TaW Jun 10 '15 at 14:44
  • No I don't. I use Cellpainting just for painting the layout (rectangles for spaces between rows, filling the background, separater between rows, etc.). The rest I let PaintContent handle. There is no manual content painting inside Cellpainting. For brushes, I follow [using Brush = new Brush(...)] for not repeating the brush, as recommended. – Kenny Jun 10 '15 at 14:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80198/discussion-between-taw-and-kenny). – TaW Jun 10 '15 at 15:01

0 Answers0