Color coding cells in datagridview

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 4 years and 2 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

Color coding cells in datagridview

Post by stevens »

Hi,

Please see viewtopic.php?t=11255

I have a similar question: I' d like to make a row lightgreen (or only the cell) in the column StatusName, but only when that status is '*New'. Datagridview is $Datagridview1.
Coloring works (if I add like *, everything is lightgreen).

Code (now working)

Code: Select all

$datagridview1_CellPainting=[System.Windows.Forms.DataGridViewCellPaintingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellPaintingEventArgs]
	
	if ($dataset.Rows.Cells['StatusName'].Value -like 'New*')
	#if ($dataset.Columns[$_.ColumnIndex].Name -like '*Done*')
	{
		
		$_.CellStyle.BackColor = 'lightgreen'
	}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Color coding cells in datagridview

Post by jvierra »

You have to specify a specific row. You are addressing all rows.

Cell paint has two properties; "RowIndex" and "ColumnIndex". You must use both.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Color coding cells in datagridview

Post by stevens »

Thanks but don't understand it.
Can you give an example?
The example of the link I provided has only Columns
if ($tableCertificates .Columns[$_.ColumnIndex].Name -eq 'notafter' -and $_.Value -gt [datetime]::Today)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Color coding cells in datagridview

Post by jvierra »

You cannot use colums to get cells or reference values.

$datagridview,Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value

The paint event is sent once for each cell being painted. You can only alter the cell being painted.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Color coding cells in datagridview

Post by jvierra »

Here are teh proeprties of teh eventargs passed to teh event handler:
https://docs.microsoft.com/en-us/dotnet ... properties
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Color coding cells in datagridview

Post by jvierra »

Here is how your code needs to work (assuming the code describes what you are trying to do).

Code: Select all

$datagridview1_CellPainting=[System.Windows.Forms.DataGridViewCellPaintingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellPaintingEventArgs]
	
    if($datagridview1.Columns[$_.ColumnIndex].Name -eq 'StatusName'){
	    if ($_.Value -like 'New*'){
		    $_.CellStyle.BackColor = 'lightgreen'
	    }else{
		    $_.CellStyle.BackColor = '<color when not matched>'
        }
    }
}
Note that it only paints cells belonging to the target column and skips all others.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Color coding cells in datagridview

Post by stevens »

Thanks, works great!
This topic is 4 years and 2 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