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
User avatar
unseenfeeling
Posts: 5
Last visit: Wed Oct 05, 2016 11:03 am

DataGridView Row Color

Post by unseenfeeling »

I am at a wall here... Never done scripting before until this project. I have two scripts, one that uses a modem to dial into another so we can manage our Routers in remote office should the WAN go down and exports the data to a CSV, then at the end combine them all. The second script is the one I am now on and have an issue with. The idea I have with using the DataGridView is to present the data where I can sort the columns and also have the row in red based on the cell value of the last column in each row. My script is a mess but I will post what I am currently using. I need some assistance getting the row coloring part of this, I have looked all over including finding pages on this forum but the results were a failure or I just didn't get what was presented. Any help is greatly appreciated! Thanks!

-edit- This is a sample of the csv file with an office that I would like to see with a red background since it has the 'NO CARRIER' result.

"Office","Date","Number","Result"
"Austin","9/27/2016","1-XXXXXXXXXX","NO CARRIER"

Set-ExecutionPolicy -ExecutionPolicy UnRestricted -Scope CurrentUser –force

Add-Type -AssemblyName System.Windows.Forms

$Form = New-Object system.Windows.Forms.Form

#Modify the form box name
$Form.Text = "OOB Results"

#Use the below to set the size of the form manually
$Form.Width = 655
$Form.Height =600

#Import CSV and detail the individual column names
$CSVSourceDir = "\\X:\OOBResults.csv"

$DataFiles = Import-CSV $CSVSourceDir

#Create DataGrid
$DataGrid = New-Object System.Windows.Forms.DataGridView

$Size = New-Object System.Drawing.Size

$Size.Width = 650

$Size.Height = 590

$DataGrid.Size = $Size

$Form.Controls.Add($DataGrid)

#Set column amount
$DataGrid.ColumnCount = 4;

$DataGrid.ColumnHeadersVisible = $true

$DataGrid.ReadOnly = $True


#Create individual columns

$DataGrid.Columns[0].DataPropertyName = "Office"

$DataGrid.Columns[1].DataPropertyName = "Date"

$DataGrid.Columns[2].DataPropertyName = "Number"

$DataGrid.Columns[3].DataPropertyName = "Result"


$DataGrid.Columns[0].Name = "Office"

$DataGrid.Columns[1].Name = "Date"

$DataGrid.Columns[2].Name = "Number"

$DataGrid.Columns[3].Name = "Result"


$DataGrid.Columns[0].HeaderText = "Office"

$DataGrid.Columns[1].HeaderText = "Date"

$DataGrid.Columns[2].HeaderText = "Number"

$DataGrid.Columns[3].HeaderText = "Result"


$DataGrid.Columns[0].Width = 150

$DataGrid.Columns[1].Width = 150

$DataGrid.Columns[2].Width = 150

$DataGrid.Columns[3].Width = 150


$dt = new-object System.Data.DataTable
$columns = $DataFiles | Get-Member -MemberType NoteProperty | select -exp Name
$columns | %{
[void]$dt.columns.add($_)
}
$DataFiles | %{
$currentRow = $_

$dr = $dt.NewRow()
$columns | %{
$dr.$_ = $currentRow.$_
}
$dt.Rows.Add($dr)
}


#Create DataGrid from DataTable
$DataGrid.DataSource = $dt

$c=$DataGrid.RowCount
for ($x=0;$x -lt $c;$x++) {
for ($y=0;$y -lt $DataGrid.Rows[$x].Cells.Count;$y++) {
$value = $DataGrid.Rows[$x].Cells[$y].Value
if ($value -eq "NO CARRIER")
{
#if Pinged cell = No change the row font color
Write-Debug "Changing color on row $x"
$DataGrid.rows[$x].DefaultCellStyle.Forecolor=[System.Drawing.Color]::FromArgb(255,255,0,0)
}
}
}

#Add DataGrid to Form
$form.Controls.Add($DataGrid)



$Form.ShowDialog()
Last edited by unseenfeeling on Wed Sep 28, 2016 8:39 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: DataGridView Row Color

Post by jvierra »

The easiest way to set the cells color after loading the grid. Just enumerate the cells of the row and set color based on key cell.

Here is a simple example: https://powershell.org/forums/topic/cha ... agridview/
User avatar
unseenfeeling
Posts: 5
Last visit: Wed Oct 05, 2016 11:03 am

Re: DataGridView Row Color

Post by unseenfeeling »

I modified my original post to show the latest position I had the rowcount added to. So far no go, but I can tell it is picking up the row numbers when I added Write-OutPut $x just to see what it was doing and it did go 0,0,0,0 1,1,1,1......
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 have no idea what that is supposed to mean. I thought you were asking about adding colors to a row.
User avatar
unseenfeeling
Posts: 5
Last visit: Wed Oct 05, 2016 11:03 am

Re: DataGridView Row Color

Post by unseenfeeling »

Yeah that's the plan, but it did not work for me. I added in the write output command to see if I had it set up right. Didn't catch any errors but no change in the colors.
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 »

Write-Output does not change colors.. What are you trying to do? Did you read all of the link I posted?

You cannot change the row color. YOU can only change cell colors. Changing the default color does not work.
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 »

Example:

$datagridview.Rows[0].Cells[0].Style.BackColor = 'Red'
User avatar
unseenfeeling
Posts: 5
Last visit: Wed Oct 05, 2016 11:03 am

Re: DataGridView Row Color

Post by unseenfeeling »

I did thank you. I posted this above last night in the original to see if you felt the placement was wrong. I apologize about the Write-OutPut, it isn't in the code I think I just have a bad habit of using it for troubleshooting purposes. I will modify the code to use $datagridview.Rows[0].Cells[0].Style.BackColor = 'Red' instead of what is currently in there to try it out.

$c=$DataGrid.RowCount
for ($x=0;$x -lt $c;$x++) {
for ($y=0;$y -lt $DataGrid.Rows[$x].Cells.Count;$y++) {
$value = $DataGrid.Rows[$x].Cells[$y].Value
if ($value -eq "NO CARRIER")
{
#if Pinged cell = No change the row font color
Write-Debug "Changing color on row $x"
$DataGrid.rows[$x].DefaultCellStyle.Forecolor=[System.Drawing.Color]::FromArgb(255,255,0,0)
}
}
}
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: DataGridView Row Color

Post by dan.potter »

You don't have to do all of that.

foreach($row in $datagrid.rows){

if($row.cells['put cell index here'].value -eq 'something')

$row.Cells['put cell index here'].Style.BackColor = 'Red'

}
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: DataGridView Row Color

Post by dan.potter »

You can also add an image column for effect :)
Untitled.png
Untitled.png (15.18 KiB) Viewed 9166 times
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