- foreach($row in $datagrid.Rows){
- if($row.Cells['Result'].Value -eq 'NO CARRIER'){
- foreach($cell in $row.Cells){
- $cell.Style.ForeColor = 'Red'
- }
- }
- }
DataGridView Row Color
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.
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.
Re: DataGridView Row Color
Well the way to enumerate a grid is as follows:
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.
Re: DataGridView Row Color
I think this is what you are trying to do. It is really much simpler than you know:
- $CSVSourceDir = 'OOBResults.csv'
- $data = Import-CSV $CSVSourceDir
- Add-Type -AssemblyName System.Windows.forms
- $form = New-Object system.Windows.forms.form
- $form.Text = 'OOB Results'
- $form.Size = '655,600'
- $form.StartPosition = 'CenterScreen'
- #Create datagrid
- $datagrid = New-Object System.Windows.forms.datagridView
- $form.Controls.Add($datagrid)
- $datagrid.Dock = 'Fill'
- $form.Controls.Add($datagrid)
- $datagrid.ReadOnly = $True
- $datagrid.AutoSizeColumnsMode = 'Fill'
- $datagrid.DataSource = [System.Collections.ArrayList]$data
- $form.add_Load({
- foreach ($row in $datagrid.Rows) {
- if ($row.Cells['Result'].Value -eq 'NO CARRIER') {
- foreach ($cell in $row.Cells) {
- $cell.Style.BackColor = 'Red'
- }
- }
- }
- })
- $form.ShowDialog()
- unseenfeeling
- Posts: 5
- Last visit: Wed Oct 05, 2016 11:03 am
Re: DataGridView Row Color
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()
$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()