Cant color datagridview

Ask your PowerShell-related questions, including questions on cmdlet development!
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 7 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
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Cant color datagridview

Post by supportMIB »

I have a datagridview populated by a datasource. If cell 2 is 'Enabled" it should paint the row green, else red.

I have used a messagebox, it is going into the if and reporting 'Enabled' but the row remains uncolored.

Code: Select all

	foreach ($row in $datagridview2.Rows)
	{	
		if ($row.cells[2].value -eq "Enabled")
		{
			[system.windows.forms.messagebox]::show(($row.cells[2].value))
			$row.DefaultCellStyle.BackColor = 'ForestGreen'
		}
		else
		{
			$row.DefaultCellStyle.BackColor = 'pink'
		}
	}
What am I missing to color the rows based on cell value?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cant color datagridview

Post by jvierra »

You have to set the color of each cell in the row by setting the Style.BackColor property.
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Cant color datagridview

Post by supportMIB »

I used this code earlier for a button and the coloring of rows worked fine...I'm not sure why it doesn't using the code I posted in the first place

Code: Select all

$button1_Click = {
	
	$selecteduser = $($datagridview2.Rows[$datagridview2.CurrentCell.RowIndex].Cells[1].value)
	
	if (((Get-ADUser $selecteduser -Properties enabled).enabled) -eq $true)
	{
		#Disable-ADAccount $selecteduser
		$($datagridview2.Rows[$datagridview2.CurrentCell.RowIndex].Cells[1].value = "Disabled")
		$datagridview2.Rows[$datagridview2.CurrentCell.RowIndex].DefaultCellStyle.BackColor = 'pink'
	}
	else
	{
		#Enable-ADAccount $selecteduser
		$($datagridview2.Rows[$datagridview2.CurrentCell.RowIndex].Cells[1].value = "Enabled")
		$datagridview2.Rows[$datagridview2.CurrentCell.RowIndex].DefaultCellStyle.BackColor = 'lawngreen'
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cant color datagridview

Post by jvierra »

Enable full row select. Be sure that the row is selected.

You cannot change a row cell color by changing the default. You have to change the cell color.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cant color datagridview

Post by jvierra »

Here is how to reset the whole row:
  1. $datagridview1.SelectedRows| %{$_.DefaultCellStyle.BackColor = 'blue'}
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: Cant color datagridview

Post by supportMIB »

This code colors the grid correctly from a button..but it doesnt work during the form load after the grid is populated...

Code: Select all

$button2_Click={
	foreach ($row in $datagridview2.rows)
	{
		
		if ($row.cells[2].value -eq "Enabled")
		{
			$row.DefaultCellStyle.BackColor = 'lawngreen'
		}
		else
		{
			$row.DefaultCellStyle.BackColor = 'pink'
		}
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Cant color datagridview

Post by jvierra »

Easy enough:
  1. $datagridview1.Rows| %{$_.DefaultCellStyle.BackColor = 'blue'}
This topic is 7 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