Change color of single cell while adding rows

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 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
it_infra
Posts: 2
Last visit: Mon Jun 21, 2021 6:30 am

Change color of single cell while adding rows

Post by it_infra »

Is it possible to change the color of a single cell while adding it, resp. directly after it?

like this:

$CurrentRow = $datagridview1.Rows.Add($Computer.ID, $Computer.OSDComputerName, $Computer.ComputerName, $Computer.MacAddress, $Computer.UUID, $UDAUser, $Computer.UILanguage, $Computer.UserLocale, $Computer.KeyboardLocale, $Computer.Description, $Computer.ModifiedBy, $Computer.Modified)

if ($Computer.Macaddress)
{
$datagridview1.Rows[$CurrentRow].Cells["MacAddress"].Style.BackColor = "Red"
}

Because I get this:
The property 'BackColor' cannot be found on this object. Verify that the property exists and can be set

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

Re: Change color of single cell while adding rows

Post by jvierra »

Change cell colors in the "paint" event where you can check the value of the cell and set the color.
Cells can always be accessed by just specifying the cell name in the row:

Row.Cells['mycell'].Style.Backcolor = 'Red'

If this doesn't work then your columns may have a different name or have not been named.
it_infra
Posts: 2
Last visit: Mon Jun 21, 2021 6:30 am

Re: Change color of single cell while adding rows

Post by it_infra »

that was actually the issue, missing name for the column.
I've added an additional one, but I can't find it anymore where I do the naming of the column in the GUI

I've done it in code until I know again where to do it in the GUI... :)

2nd.
My original code works now, but can I refer simpler to the just created row instead of doing this:

$CurrentRow = $datagridview1.Rows.Add($Computer.ID, $Computer.OSDComputerName, $Computer.ComputerName, $Computer.MacAddress, $Computer.UUID, $UDAUser, $Computer.UILanguage, $Computer.UserLocale, $Computer.KeyboardLocale, $Computer.Description, $Computer.ModifiedBy, $Computer.Modified)

$datagridview1.Rows[$CurrentRow].Cells["UUID"].Style.BackColor = 'LightGray'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Change color of single cell while adding rows

Post by jvierra »

No. The index is returned just for this and other reasons.

If you autogenerate the grid using a proper datasource or a datatable then the names will be assigned automatically.
The name property is part of the column object in the designer. You assign the name when creating a new column.
This topic is 4 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