Cell painting Efficiency

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 2 years and 3 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
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Cell painting Efficiency

Post by mattys »

I have a cellpainting event in my Datagridview.

Code: Select all

		#Color Backcolor according to Impact Level#
	foreach ($row in $datagridviewUpcomingNews.rows)
	{
		#High
		if ($row.cells['Impact'].value -eq 'HIGH')
		{
			
			{
				$cell.Style.BackColor = [System.Drawing.Color]::FromArgb(16, 132, 198)
				$cell.Style.ForeColor = 'White'
			}
		}
		#Medium
		if ($row.cells['Impact'].value -eq 'Medium')
		{
			foreach ($cell in $row.Cells)
			{
				$cell.Style.BackColor = 'IndianRed'
				$cell.Style.ForeColor = 'White'
			}
		}
	}
Is this the most efficient way to paint rows?
I feel like it is not.
Any pointers would be greatly appreciated!!
Thank you so much
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cell painting Efficiency

Post by jvierra »

It is the only correct way to do this. Any other way leads to advanced coding issues. In the end it depends on what you are trying to accomplish.

In WinForms efficiency is built in if you follow the rules and understand how forms are intended to work. Events expose methods and properties that are specific to the state of the form/control at that moment in the flow of events. Everything else is just the programmer setting up the states and responding during the events.
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: Cell painting Efficiency

Post by mattys »

OK
Thank you for confirmation jvierra and to be honest, your overall contribution to this board.

Take care
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cell painting Efficiency

Post by jvierra »

You are very welcome Matty. Glad to be of help and have a nice holiday season.
This topic is 2 years and 3 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