RowPainting example?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

RowPainting example?

Post by stevens »

Hi,

I'd need to paint a row when certain value in a cell.
Cellpainting is working fine, but can't find rowpainting example.

This is my cellpainting example:
if ($datagridview1.Columns[$_.ColumnIndex].Name -eq 'Enabled')
{
if ($_.Value -eq 'yes')
{
$_.CellStyle.BackColor = 'lightgreen'
}
}

Tried with options like $_.PaintCells() but can't make it work.

Please advise.
J
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: RowPainting example?

Post by jvierra »

RowPrePaint occurs once per row. You an only set the cell back color and contents per cell and it must all be done once. The cell paint events will override any changes made in rowprepaint. It is recommended that all cells that need color changes based on another cell be painted in the CellPainting event. For what you are asking this is all you need to do. Using row events will cause you top do all of the creation of each row by hand. It is not used for most normal adjustments to cell contents and format.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: RowPainting example?

Post by stevens »

Thanks, but my example does not work. Do you have one which does work?
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: RowPainting example?

Post by stevens »

Note: I got error
- Index operation failed; the array index evaluated to null..Exception.Message at line 16293

I use
if ($datagridviewAll.Columns[$_.ColumnIndex].Name -eq 'MyValue')
{
try
{
if ($($_.Value -ne $null) -or $($_.Value -ne [System.dbnull]::Value) -or $($_.Value -ne 'MyValue'))
{
if ($_.Value -eq 'True')
{
$_.CellStyle.BackColor = 'lightgrey'
}
}
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: RowPainting example?

Post by stevens »

Never mind, figured it out!
This topic is 3 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked