Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Although the cellformatting event is nicely optimised to only work on visible rows on-screen - it seems to be also the same for cells that are off-screen.

Having 12-16 columns with several of these off-screen to the right of the visible cell/columns, I am setting the backgroundcolor property of a row style via a column/cell - the cellformatting does not fire... It fires and changes the row of the data once I scroll over to the right once the cell is visible...

To explain this better:
I have a datagridview with invoice information that I want to highlight in red when it a date in the row is within a couple of days of being outstanding. Now - the code works fine on my widescreen as all the columns show. Rows turn red as they should etc.
However, on a lower-resolution monitor, even when the form is maximised, my trigger-cell disappears off to the right and cellformatting doesnt pick it up and therefore does not paint the row red as it should.

I have tried having two hidden cells over to the left but it looks like cellformatting ignores invisible cells for obvious reasons (its not going to paint something invisible). The only alternative and then it gets ugly, is to move the date outstanding cell pretty much to within the first 2 or 3, maybe 4 columns instead of where it is at the moment.

I have tried a few searches in Google but I suspect I may be using the incorrect search term "datagridview cellformatting not working when cell is to the right" and it nothing that I can see useful for me.

I guess another solution, is to utilise my "invoice number" and do an on-the-fly query to check if the invoice record date is within the boundariesI have set ,but i was hoping not to do queries that are hitting the database while a user scrolls around either.

Is this a bug with .net ? Im using VS2015 with 4.6 of the framework.
or is there a non-paint-enabled "cellformatting type" event that also gets triggered so i can examine any cell contents otherwise the performance gain that DGV gets out of not looking at all cells means a much bigger hit (for me anyway) by having to query the database for every record, per page of records.

What I have tried:

Various google searches without success.
Updated 4-Oct-18 4:21am
[no name] 24-Sep-18 11:47am    
What's the "problem"?

Cell "formatting" occurs when the cell is "painted"; there is no point in "painting", when the cell is "off screen" (i.e. not visible). At the very least, this is done for performance.
Member 12561559 24-Sep-18 12:04pm    
Have revised my question, I didnt make it clearer enough - thanks and I hope you may be able to help ! :)
Alek Massey 25-Sep-18 15:06pm    
If formatting of visible objects is dependent on whether another, potentially hidden object is visible, you need to to remove that dependency.
Member 12561559 4-Oct-18 7:50am    
The only thing I can think of is to move the Invoice Due date into one of the first 3 cells so it gets picked up by cellformatting and is an almost guarantee that its not going to fall off out of visible row.

1 solution

If your DataGridView or the columns that decide cell style are read-only, you should change DefaultCellStyle of rows in RowsAdded event. This event occurs only once when a new row is added. The condition you are examining should be evaluated then and DefaultCellStyle of the row should be set there.

If the datagridview is editable or the cells the condition is based on are editable then use the CommitEdit event to change DefaultCellStyle
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900