Apply color to single cell based on [int] value

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 4 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
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Apply color to single cell based on [int] value

Post by pjbuckley »

this code isnt updating the color of the cells:

Code: Select all


$DGV7_CellPainting = [System.Windows.Forms.DataGridViewCellPaintingEventHandler] {
	if ($DGV7.rows.cells["notdocumented"].value -ne 0) {
		$DGV7.rows.cells["notdocumented"].style.backcolor = '#ff0f0f'
	}
}
any ideas?

ANSWER:

I was missing the following code:

$DGV7.add_CellPainting($DGV7_CellPainting)
Last edited by pjbuckley on Wed Dec 11, 2019 10:07 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Apply color to single cell based on [int] value

Post by jvierra »

A "color" in NOT an int. It is an object. It can be assigned by a string or an int.

You cannot compare an object to a string number:

The following is wrong.
$DGV7.rows.cells["notdocumented"].value -ne '0')

This is correct:

$DGV7.rows.cells["notdocumented"].value -ne 'Black')
$DGV7.rows.cells["notdocumented"].value -ne 0)
$DGV7.rows.cells["notdocumented"].value -ne [System.Drawing.Color]::Black)
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Apply color to single cell based on [int] value

Post by pjbuckley »

The value in the cell is an integer. The code is supposed to change the color of the cell based upon the value in the cell.

and yes ive tried with and without the quotes around the zero and still no dice.
Last edited by pjbuckley on Tue Dec 10, 2019 4:49 pm, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Apply color to single cell based on [int] value

Post by jvierra »

I think I have guessed at what you are trying to ask and od:

This is how to use the "Paint" event to change a cell in a specific column.

Code: Select all

$DGV7_CellPainting = [System.Windows.Forms.DataGridViewCellPaintingEventHandler] {
    if($dgv7.Columns[$_.ColumnIndex].Name -eq 'notdocumented'){
	    if($_.value){
		    $_.style.backcolor = '#ff0f0f'
	    }
    }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Apply color to single cell based on [int] value

Post by jvierra »

pjbuckley wrote: Tue Dec 10, 2019 4:43 pm The value in the cell is an integer. The code is supposed to change the color of the cell based upon the value in the cell.
You cannot compare an integer to a string so try to break the habit. It will cause you issues forever. An integer cannot be quoted or it will become a string that contains the character value of the character

Try this at a prompt.
[int]('0'[0])

This is what can happen in a comparison. Coercion will not work correctly under many circumstances especially when comparing to objects.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Apply color to single cell based on [int] value

Post by pjbuckley »

Im having trouble finding the $_.value property on a column. I see it on the rows but not columns

Are you saying that i should change my integers into strings when comparing them?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Apply color to single cell based on [int] value

Post by jvierra »

I am saying that you cannot quote a number in a comparison with an int. That is why I removed your quotes.

The code I posted does what you want assuming that the information you have provided is correct. Most of your code was not correct and was making incorrect assumptions.
The paint event is set once for each cell unt9il the paint cascade is complete. We detect the column which tells us the correct cell we want to target. The "$_" is the cell object that is passed to the event.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Apply color to single cell based on [int] value

Post by jvierra »

pjbuckley wrote: Tue Dec 10, 2019 5:00 pm Im having trouble finding the $_.value property on a column.
The "Value" property is a property of a cell and not a row. "Rows" is a collection/ It is not a Row.

Start by reading and studying the DGV object here: https://docs.microsoft.com/en-us/dotnet ... mework-4.8

You may have trouble with that if you haven't read this: https://docs.microsoft.com/en-us/dotnet ... mework-4.8 which is the grandfather of all other controls.
pjbuckley
Posts: 31
Last visit: Mon Jan 06, 2020 12:21 pm

Re: Apply color to single cell based on [int] value

Post by pjbuckley »

Thanks. I already reference those articles regularly. they are useful but havent helped me fix my problem.

I learn by working examples. I take bits of code that work and apply them to my scripts.

Since that is all in C# its mostly gobbledy-gook to me. i dont have a background in development and dont develop every day for my job. This is a one off that i need to get done because i am already WAY over budget and losing money every day i cant get these (apparently) simple tasks in a much bigger project done.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Apply color to single cell based on [int] value

Post by jvierra »

I didn't put them up for reference. You need to read the full article carefully and review the code and what it is showing you.

It is clear that you haven't learned how to use the documentation and you haven't read the doc on the event. You need to do this to understand how to use forms. That is why Sapien links these articles to the control in the toolbox. Just click on the control then find the event and read the details of the args and usage.

The code I posted is how the event is intended to work. If you do not understand why I changed your code in the way I did then you need to read the docs until you understand.

You cannot get working examples that are usable anywhere except for very trivial issues. The docs on the "Graphics" object and events must be studied and learned. There are no good examples If I had the time I would write a documented example but that would take about 10 pages and would assume a basic understanding of forms, controls and events at a technical level as well as Windows and the Windows Graphics system as exposed by the "System.Drawing" namespace and the "System.Drawing.Graphics" namespace and other namespaces that are used by the WinForms system.
This topic is 4 years and 4 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