DataGridView Row Color

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 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView Row Color

Post by jvierra »

Well the way to enumerate a grid is as follows:
  1. foreach($row in $datagrid.Rows){
  2.     if($row.Cells['Result'].Value -eq 'NO CARRIER'){
  3.         foreach($cell in $row.Cells){
  4.             $cell.Style.ForeColor = 'Red'
  5.                }
  6.         }
  7. }
No need for indexing or guessing at column numbers. Forms has been designed so that we don't need to figure things out in code. We just use the objects name to find it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView Row Color

Post by jvierra »

I think this is what you are trying to do. It is really much simpler than you know:
Code: [Select all] [Expand/Collapse] [Download] (Demo-SimpleGrid.ps1)
  1. $CSVSourceDir = 'OOBResults.csv'
  2. $data = Import-CSV $CSVSourceDir
  3.  
  4. Add-Type -AssemblyName System.Windows.forms
  5. $form = New-Object system.Windows.forms.form
  6. $form.Text = 'OOB Results'
  7. $form.Size = '655,600'
  8. $form.StartPosition = 'CenterScreen'
  9.  
  10. #Create datagrid
  11. $datagrid = New-Object System.Windows.forms.datagridView
  12. $form.Controls.Add($datagrid)
  13. $datagrid.Dock = 'Fill'
  14. $form.Controls.Add($datagrid)
  15. $datagrid.ReadOnly = $True
  16. $datagrid.AutoSizeColumnsMode = 'Fill'
  17. $datagrid.DataSource = [System.Collections.ArrayList]$data
  18.  
  19. $form.add_Load({
  20.     foreach ($row in $datagrid.Rows) {
  21.         if ($row.Cells['Result'].Value -eq 'NO CARRIER') {
  22.             foreach ($cell in $row.Cells) {
  23.                 $cell.Style.BackColor = 'Red'
  24.             }
  25.         }
  26.     }
  27. })
  28.  
  29. $form.ShowDialog()
User avatar
unseenfeeling
Posts: 5
Last visit: Wed Oct 05, 2016 11:03 am

Re: DataGridView Row Color

Post by unseenfeeling »

Got the results I wanted. Tried the code you had provided and it still didn't work even after having one of our more script savvy guys take a look at it. I had a hunch it was the way the file was imported from the earlier failures before I came here so with some help I changed it up a bit. Came down to the order and having excess steps in the script. So as you can see I have some of the examples given so much appreciated.


$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(680,535)
$Date = Get-Item "\\PHQWOOB1\Combined\OOBResults.csv" | Foreach {$_.LastWriteTime.ToShortDateString()}
$Form.Text = "OOB Results $Date"
$Form.ShowInTaskbar = $True
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.AutoSizeMode = "GrowAndShrink"
$Form.SizeGripStyle = "Hide"
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(660,500)
$dataGridView.AllowUserToAddRows = $False
$form.Controls.Add($dataGridView)


$dataGridView.ColumnCount = 4
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns[0].Name = "Office"
$DataGridView.Columns[0].Width = 150
$dataGridView.Columns[1].Name = "Date"
$DataGridView.Columns[1].Width = 150
$dataGridView.Columns[2].Name = "Number"
$DataGridView.Columns[2].Width = 150
$dataGridView.Columns[3].Name = "Result"
$dataGridView.Columns[3].width = 150

$CSVSourceDir = "\\PHQWOOB1\Combined\OOBResults.csv"
$data = Import-CSV $CSVSourceDir

$data | foreach {
$dataGridView.Rows.Add($_.Office,$_.Date,$_.Number,$_.Result) | out-null
}

foreach ($Row in $dataGridView.Rows) {
if ($Row.Cells[2].Value -eq '') {
$Row.defaultcellstyle.backcolor = "yellow"
}
elseif ($Row.Cells[3].Value -like "*NO CARRIER*") {
$Row.defaultcellstyle.backcolor = "Crimson"
}
elseif ($Row.Cells[3].Value -like "*CONNECT 1200/ARQ*") {
$Row.defaultcellstyle.forecolor = "red"
}
elseif ($Row.Cells[3].Value -like "*CONNECT 2400/ARQ*") {
$Row.defaultcellstyle.forecolor = "red"
}
elseif ($Row.Cells[3].Value -like "*BUSY*") {
$Row.defaultcellstyle.backcolor = "Crimson"
}else {
$row.defaultcellstyle.backcolor = "white"
}
}

[void]$form.ShowDialog()
This topic is 7 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