DataGridView sometime Colored sometime not

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
Locked
User avatar
monoeagle
Posts: 107
Joined: Tue Oct 13, 2015 9:10 am

DataGridView sometime Colored sometime not

Post by monoeagle » Fri Sep 15, 2017 3:45 am

Hi@All,

I think it's a littlebit OT, because I can't use here the PSS and have to use MS ISE.
But if someone can give a hint it would be extremly nice.
I have a curious behavier with my GUI.

Environment: W10, PS5

If I open and start the script from ISE the cells are colored as it should be. BUT just from the second Run/Start.
Services_farbig.JPG
Services_farbig.JPG (90 KiB) Viewed 754 times
If I run the script nativ without ISE the cells are white on every start.
Services_sw.JPG
Services_sw.JPG (93.02 KiB) Viewed 754 times
But Why?

Script, cutted down

Code: Select all

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null


# define tablename
$TableName = "DataTable01"

# create table
$global:table = New-Object system.Data.DataTable "$TableName"

# define columns
$col0 = New-Object system.Data.DataColumn Auswahl, ([bool])
$col1 = New-Object system.Data.DataColumn Servername, ([string])
$col2 = New-Object system.Data.DataColumn IP, ([string])

$col3 = New-Object system.Data.DataColumn BlsRS, ([string])
$col4 = New-Object system.Data.DataColumn CltPS, ([string])
$col5 = New-Object system.Data.DataColumn CoreS, ([string])
$col6 = New-Object system.Data.DataColumn DistS, ([string])
$col7 = New-Object system.Data.DataColumn mCast, ([string])
$col8 = New-Object system.Data.DataColumn PxeS, ([string])
$col9 = New-Object system.Data.DataColumn TftfS, ([string])
$col10 = New-Object system.Data.DataColumn DsmRS, ([string])
$col11 = New-Object system.Data.DataColumn DsmTL, ([string])

$col12 = New-Object system.Data.DataColumn FSAUA, ([string])
$col13 = New-Object system.Data.DataColumn FSMA, ([string])
$col14 = New-Object system.Data.DataColumn FSNRB, ([string])
$col15 = New-Object system.Data.DataColumn FSORS, ([string])
$col16 = New-Object system.Data.DataColumn FSWUI, ([string])
$col17 = New-Object system.Data.DataColumn FSGHKS, ([string])

$col18 = New-Object system.Data.DataColumn LW_C, ([string])
$col19 = New-Object system.Data.DataColumn LW_E, ([string])

$col20 = New-Object system.Data.DataColumn NiCfgLcl, ([string])
$col21 = New-Object system.Data.DataColumn NiCfgSrv, ([string])

$col22 = New-Object system.Data.DataColumn Beschreibung, ([string])

#Add the Columns
$global:table.columns.add($col0)
$global:table.columns.add($col1)
$global:table.columns.add($col2)
$global:table.columns.add($col3)
$global:table.columns.add($col4)
$global:table.columns.add($col5)
$global:table.columns.add($col6)
$global:table.columns.add($col7)
$global:table.columns.add($col8)
$global:table.columns.add($col9)
$global:table.columns.add($col10)
$global:table.columns.add($col11)
$global:table.columns.add($col12)
$global:table.columns.add($col13)
$global:table.columns.add($col14)
$global:table.columns.add($col15)
$global:table.columns.add($col16)
$global:table.columns.add($col17)
$global:table.columns.add($col18)
$global:table.columns.add($col19)

$global:table.columns.add($col20)
$global:table.columns.add($col21)
$global:table.columns.add($col22)


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

$Form.minimumSize = New-Object System.Drawing.Size(1400,768) 
#$Form.maximumSize = New-Object System.Drawing.Size(1024,768) 

#$Form.WindowState = "minimized" 

$Form.Text = "dummy"
$Form.startposition = "centerscreen"

$form.AutoSize = $False
$form.MinimizeBox = $False
$form.MaximizeBox = $true

$MS_Main = new-object System.Windows.Forms.MenuStrip
$ActionToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
$LoadDataFromFileToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem


$MS_Main.Items.AddRange(@($ActionToolStripMenuItem))
$MS_Main.Location = new-object System.Drawing.Point(0, 0)
$MS_Main.Name = "MS_Main"
$MS_Main.Size = new-object System.Drawing.Size(354, 24)
$MS_Main.TabIndex = 0
$MS_Main.Text = "menuStrip1"


# ActionToolStripMenuItem
$ActionToolStripMenuItem.DropDownItems.AddRange(@($LoadDataFromFileToolStripMenuItem))
$ActionToolStripMenuItem.Name = "ActionToolStripMenuItem"
$ActionToolStripMenuItem.Size = new-object System.Drawing.Size(51, 20)
$ActionToolStripMenuItem.Text = "&Action"
#

#region Load Data from File Click Event
$LoadDataFromFileToolStripMenuItem.Name = "LoadDataFromFileToolStripMenuItem"
$LoadDataFromFileToolStripMenuItem.Size = new-object System.Drawing.Size(51, 20)
$LoadDataFromFileToolStripMenuItem.Text = "Load Data From File"

$LoadDataFromFileToolStripMenuItem.Add_Click( { 
$global:table.clear()
$global:table.ReadXml("C:\temp\Library.xml")
$dataGridView.Update()
} )
#endregion

#Tab Control 
$TabControl = New-object System.Windows.Forms.TabControl
$Overview = New-Object System.Windows.Forms.TabPage
$tabControl.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 75
$System_Drawing_Point.Y = 85
$tabControl.Location = $System_Drawing_Point
$tabControl.Name = "tabControl"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 300
$System_Drawing_Size.Width = 575
$tabControl.Dock = "Fill"
$tabControl.Size = $System_Drawing_Size
$form.Controls.Add($tabControl)

#Overview Page
$Overview.DataBindings.DefaultDataSourceUpdateMode = 0
$Overview.UseVisualStyleBackColor = $True
$Overview.Name = "OverviewPage"
$Overview.Text = "Overview”
$tabControl.Controls.Add($Overview)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 600
$System_Drawing_Size.Height = 433
$dataGridView.Size = $System_Drawing_Size
$dataGridView.DataBindings.DefaultDataSourceUpdateMode = 0
$dataGridView.Name = "dataGridView"
$dataGridView.DataMember = ""
$dataGridView.TabIndex = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$dataGridView.Location = $System_Drawing_Point
$dataGridView.Dock ="Fill"
$datagridview.AutoSizeColumnsMode = 'Fill'
$datagridview.DataSource = $global:table
$dataGridView.AllowUserToAddRows = $false
$dataGridView.SelectionMode = 'FullRowSelect'
$dataGridView.MultiSelect = $false
$dataGridView.add_CellFormatting($dataGridView_CellFormatting)

function Enable-DataGridViewDoubleBuffer {
 param ([Parameter(Mandatory = $true)]
 [System.Windows.Forms.DataGridView]$grid,
 [switch]$Disable)

 $type = $grid.GetType();
 $propInfo = $type.GetProperty("DoubleBuffered", ('Instance','NonPublic'))
 $propInfo.SetValue($grid, $Disable -eq $false, $null)
 }

$Overview.Controls.Add($dataGridView)

$dataGridView_CellFormatting=[System.Windows.Forms.DataGridViewCellFormattingEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellFormattingEventArgs]
		#TODO: Place custom script here

		if (($_.ColumnIndex -gt 2) -and ($_.ColumnIndex -lt 18) ) 
		{
			if ($_.Value -eq "running")
			{
				$_.CellStyle.BackColor = 'green'
				$_.CellStyle.ForeColor = 'white'
			}
			
			if ($_.Value -eq "stopped")
			{
				$_.CellStyle.BackColor = 'Red'
				$_.CellStyle.ForeColor = 'white'
			}

			if ($_.Value -eq "NA" )
			{
				$_.CellStyle.BackColor = 'blue'
				$_.CellStyle.ForeColor = 'white'
			}

		}


		if ($_.ColumnIndex -eq 18) {
			if ([int]($_.Value) -le 30)
			{
				$_.CellStyle.BackColor = 'Orange'
				$_.CellStyle.ForeColor = 'white'
			}else{
	            $_.CellStyle.BackColor = 'Green'
				$_.CellStyle.ForeColor = 'white'
            }
        }
        
		if ($_.ColumnIndex -eq 19) {
			if ([int]($_.Value) -le 260)
			{
				$_.CellStyle.BackColor = 'Orange'
				$_.CellStyle.ForeColor = 'white'
			}else{
	            $_.CellStyle.BackColor = 'Green'
				$_.CellStyle.ForeColor = 'white'
            }
        }

}


function OnFormClosing_MenuForm($Sender,$e){ 
    ($_).Cancel= $False
}

$Form.Add_FormClosing( { OnFormClosing_MenuForm $Form $EventArgs} )
$Form.Controls.Add($MS_Main)
$Form.MainMenuStrip = $MS_Main

$Form.Add_Shown({
    Enable-DataGridViewDoubleBuffer -Grid $dataGridView
    $dataGridView.Columns[22].AutoSizeMode = 'Fill'
    $Form.Activate()
})

$Form.ShowDialog() | Out-Null

#Free ressources
$Form.Dispose()
the appropiate XML in C:\temp\Library.xml

Code: Select all

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="DataTable01" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="DataTable01">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Auswahl" type="xs:boolean" minOccurs="0" />
                <xs:element name="Servername" type="xs:string" minOccurs="0" />
                <xs:element name="IP" type="xs:string" minOccurs="0" />
                <xs:element name="BlsRS" type="xs:string" minOccurs="0" />
                <xs:element name="CltPS" type="xs:string" minOccurs="0" />
                <xs:element name="CoreS" type="xs:string" minOccurs="0" />
                <xs:element name="DistS" type="xs:string" minOccurs="0" />
                <xs:element name="mCast" type="xs:string" minOccurs="0" />
                <xs:element name="PxeS" type="xs:string" minOccurs="0" />
                <xs:element name="TftfS" type="xs:string" minOccurs="0" />
                <xs:element name="DsmRS" type="xs:string" minOccurs="0" />
                <xs:element name="DsmTL" type="xs:string" minOccurs="0" />
                <xs:element name="FSAUA" type="xs:string" minOccurs="0" />
                <xs:element name="FSMA" type="xs:string" minOccurs="0" />
                <xs:element name="FSNRB" type="xs:string" minOccurs="0" />
                <xs:element name="FSORS" type="xs:string" minOccurs="0" />
                <xs:element name="FSWUI" type="xs:string" minOccurs="0" />
                <xs:element name="FSGHKS" type="xs:string" minOccurs="0" />
                <xs:element name="LW_C" type="xs:string" minOccurs="0" />
                <xs:element name="LW_E" type="xs:string" minOccurs="0" />
                <xs:element name="NiCfgLcl" type="xs:string" minOccurs="0" />
                <xs:element name="NiCfgSrv" type="xs:string" minOccurs="0" />
                <xs:element name="Beschreibung" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <DataTable01>
    <Auswahl>false</Auswahl>
    <Servername>Server1</Servername>
    <IP>133.21.122.111</IP>
    <BlsRS>NA</BlsRS>
    <CltPS>Running</CltPS>
    <CoreS>Running</CoreS>
    <DistS>Running</DistS>
    <mCast>Running</mCast>
    <PxeS>Running</PxeS>
    <TftfS>Running</TftfS>
    <DsmRS>Running</DsmRS>
    <DsmTL>Running</DsmTL>
    <FSAUA>Running</FSAUA>
    <FSMA>Running</FSMA>
    <FSNRB>Running</FSNRB>
    <FSORS>Running</FSORS>
    <FSWUI>Running</FSWUI>
    <FSGHKS>Running</FSGHKS>
    <LW_C>34,70</LW_C>
    <LW_E>309,54</LW_E>
    <NiCfgLcl>12.09.2017 10:22:16</NiCfgLcl>
    <NiCfgSrv>12.09.2017 10:22:15</NiCfgSrv>
    <Beschreibung />
  </DataTable01>
  <DataTable01>
    <Auswahl>false</Auswahl>
    <Servername>Server2</Servername>
    <IP>111.111.110.111</IP>
    <BlsRS>NA</BlsRS>
    <CltPS>Running</CltPS>
    <CoreS>Running</CoreS>
    <DistS>Running</DistS>
    <mCast>Running</mCast>
    <PxeS>Stopped</PxeS>
    <TftfS>Running</TftfS>
    <DsmRS>Running</DsmRS>
    <DsmTL>Running</DsmTL>
    <FSAUA>Running</FSAUA>
    <FSMA>Running</FSMA>
    <FSNRB>Running</FSNRB>
    <FSORS>Running</FSORS>
    <FSWUI>Running</FSWUI>
    <FSGHKS>Running</FSGHKS>
    <LW_C>35,24</LW_C>
    <LW_E>309,39</LW_E>
    <NiCfgLcl>12.09.2017 10:22:16</NiCfgLcl>
    <NiCfgSrv>12.09.2017 10:22:15</NiCfgSrv>
    <Beschreibung />
  </DataTable01>
</NewDataSet>
regards
Attachments
_cuttedDown.ps1
(7.82 KiB) Downloaded 32 times

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView sometime Colored sometime not

Post by jvierra » Fri Sep 15, 2017 4:17 am

No data file.

User avatar
monoeagle
Posts: 107
Joined: Tue Oct 13, 2015 9:10 am

Re: DataGridView sometime Colored sometime not

Post by monoeagle » Fri Sep 15, 2017 5:10 am

added
Attachments
Library.xml
(3.77 KiB) Downloaded 31 times

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView sometime Colored sometime not

Post by jvierra » Fri Sep 15, 2017 5:35 am

Your event is not attached to the grid in the code you posted.

Copy all files to new folder and close all copies of PS. Run tests from fresh copy of PS and ISE. Both have no color.

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView sometime Colored sometime not

Post by jvierra » Fri Sep 15, 2017 5:36 am

Move this line to AFTER the declaration of the event script block:

$dataGridView.add_CellFormatting($dataGridView_CellFormatting)

User avatar
monoeagle
Posts: 107
Joined: Tue Oct 13, 2015 9:10 am

Re: DataGridView sometime Colored sometime not

Post by monoeagle » Fri Sep 15, 2017 5:49 am

THX it runs.

Locked