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.
Post Reply
User avatar
stevens
Posts: 382
Joined: Wed Jun 15, 2016 2:26 am

Color coding cells in datagridview

Post by stevens » Mon Jan 27, 2020 6:09 am

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: 14145
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Color coding cells in datagridview

Post by jvierra » Mon Jan 27, 2020 9:37 am

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: 382
Joined: Wed Jun 15, 2016 2:26 am

Re: Color coding cells in datagridview

Post by stevens » Tue Jan 28, 2020 8:41 am

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: 14145
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Color coding cells in datagridview

Post by jvierra » Tue Jan 28, 2020 9:19 am

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: 14145
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Color coding cells in datagridview

Post by jvierra » Tue Jan 28, 2020 9:27 am

Here are teh proeprties of teh eventargs passed to teh event handler:
https://docs.microsoft.com/en-us/dotnet ... properties

jvierra
Posts: 14145
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Color coding cells in datagridview

Post by jvierra » Tue Jan 28, 2020 9:36 am

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: 382
Joined: Wed Jun 15, 2016 2:26 am

Re: Color coding cells in datagridview

Post by stevens » Tue Feb 04, 2020 6:21 am

Thanks, works great!

Post Reply