How to highlight a specific row in a datagridview

I am building a form with a datagridview and a button to add items to the datagrid view.  I would like to highlight (and optionally center and not select) the row of the just added item to verify for the user what has changed.  I can't seem to get it working:

 $dataGridView1.datasource = [System.Collections.ArrayList]@($list)
 foreach ($Row in $dataGridView1.Rows) {
       if ($Row.Name -eq $newitem.name) {
           dataGridView1.Rows[$Row.Index].DefaultCellStyle.BackColor = Yellow
       } else {
           dataGridView1.Rows[$Row.Index].DefaultCellStyle.BackColor = white;
       }
   }
 

gives me no change.  I suspect the problem is my attempt to reference properties of the Row (e.g. $Row.name), but I don't know.

I would really love a little assistance if anyone is willing.

Thanks.

December 22nd, 2011 4:30pm

since $row is the row item, you can just set its color, you dont need to do it from $datagridview1   $row.defaultcellstyle.backcolor =    
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2011 6:44pm

Hello cascomp,

Currently, I agree with Justin. You can change the cell background color when you add a item.

I tried to accomplish your goal by myself, here is the result. I hope it can give you some ideas about your problem.

If you have any suggestions or ideas, please feel free to tell me.

 

Best Regards,

Greg Gu

 

Code:

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 12/23/2011 9:44 AM
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$textBox1 = New-Object System.Windows.Forms.TextBox
$button1 = New-Object System.Windows.Forms.Button
$dataGridView1 = New-Object System.Windows.Forms.DataGridView
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button1_OnClick= 
{
	$newData = $textBox1.Text
	$rowIndex = $dataGridView1.Rows.Add($newData)
	$dataGridView1.Rows.Item($rowIndex - 1).DefaultCellStyle.BackColor = "White"
	$dataGridView1.Rows.Item($rowIndex).DefaultCellStyle.BackColor = "LightGreen"
	$dataGridView1.FirstDisplayedScrollingRowIndex = $rowIndex
}

$handler_textBox1_TextChanged= 
{
#TODO: Place custom script here

}

$handler_form1_Load= 
{
	$data = "blahblahblah"
	$dataGridView1.Columns.Add("ColumnA","Column A")
	$dataGridView1.Columns.Add("ColumnB","Column B")
	$dataGridView1.ReadOnly = $true
	$dataGridView1.MultiSelect = $false
	$dataGridView1.Rows.Add($data)
	$dataGridView1.Rows.Item(0).Selected = $false
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 262
$System_Drawing_Size.Width = 284
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Primal Form"
$form1.add_Load($handler_form1_Load)

$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 198
$textBox1.Location = $System_Drawing_Point
$textBox1.Name = "textBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 259
$textBox1.Size = $System_Drawing_Size
$textBox1.TabIndex = 2
$textBox1.add_TextChanged($handler_textBox1_TextChanged)

$form1.Controls.Add($textBox1)


$button1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 227
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 259
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 1
$button1.Text = "Add"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

$dataGridView1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$dataGridView1.Location = $System_Drawing_Point
$dataGridView1.Name = "dataGridView1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 170
$System_Drawing_Size.Width = 260
$dataGridView1.Size = $System_Drawing_Size
$dataGridView1.TabIndex = 0

$form1.Controls.Add($dataGridView1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm


Test result:

December 23rd, 2011 3:02am

How to change the color of row in Datagridview. Eg. Am getting a remote server services by get-service.  I want to change the color of services that are not running. Am using the below function to get the services.

Please help me it would be useful

    

function Get-SystemInfo
{

    $array = New-Object System.Collections.ArrayList
$Script:SystemInfo = Get-WmiObject win32_service -ComputerName $textbox1Computername.text | select DisplayName,Name,state,status,startmode | Sort-Object state,startmode,DisplayName
$array.AddRange($SystemInfo)
$datagridview1.DataSource = $array

}

Free Windows Admin Tool Kit Click here and download it now
April 6th, 2015 10:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics