WPF DataGrid Cell Selection - wpf

I'm trying to search a WPF DataGrid via PowerShell, select and highlight the cells, each one in turn, that contains the search keyword. The goal is to 1) search the DataGrid, 2) select and highlight the cell that contains search keyword while moving the focus/scrolling to the row that contains column/cell and 3) repeat the same for any other columns/cells that may contain the same keyword.
I put together a sample script (see below) to demonstrate what I have done so far. In the sample script shown below, I was able to search and find the row contains the keyword and scroll to that row. However, I couldn't figure out how to highlight the cell that contains the keyword on that row.
The sample data shown below has pre-defined columns for demonstration purpose. However, the actual data returned from backend is dynamic such that the number of columns, title of the columns would vary and any column may contain the keyword. How do we select the cell that contains the keyword? Is there any better approach to achieve this overall goal? Thanks in advance for the help.
[xml]$xaml=#"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
Title="MainWindow" Height="175" Width="550">
<Grid>
<TextBox x:Name="tb_Search" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149"/>
<Button x:Name="bt_Search" Content="Search" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" IsDefault="True" Height="22" Margin="165,10,0,0" />
<DataGrid x:Name="dg" Margin="10,45,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="100" Height="Auto" Width="Auto" ColumnWidth="Auto" AlternationCount="1" IsReadOnly="True" SelectionMode="Extended" SelectionUnit="Cell" Background="White" />
</Grid>
</Window>
"#
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)
#Turn XAML into PowerShell objects
$xaml.SelectNodes("//*[#*[contains(translate(name(.),'n','N'),'x:Name')]]") | ForEach-Object{
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
}
#sample data
$DataSet = New-Object System.Data.DataSet
$Table = $DataSet.Tables.Add("Table")
$Properties = #("Country","Capital","Population")
$Properties | foreach {
$Column = New-Object System.Data.DataColumn($_)
$Table.Columns.Add($Column)
}
$Null=$Table.Rows.Add("China PR","Beijing","20,693,000")
$Null=$Table.Rows.Add("India","New Delhi","16,787,949")
$Null=$Table.Rows.Add("Japan","Tokyo","13,189,000")
$Null=$Table.Rows.Add("Philippines","Manila","12,877,253")
$Null=$Table.Rows.Add("Russia","Moscow","11,541,000")
$Null=$Table.Rows.Add("Egypt","Cairo","10,230,350")
$Null=$Table.Rows.Add("USA","Washington, D.C","658,893")
$Null=$Table.Rows.Add("China PR","Beijing","20,693,000")
$Null=$Table.Rows.Add("India","New Delhi","16,787,949")
$Null=$Table.Rows.Add("Japan","Tokyo","13,189,000")
$Null=$Table.Rows.Add("Philippines","Manila","12,877,253")
$Null=$Table.Rows.Add("Russia","Moscow","11,541,000")
$Null=$Table.Rows.Add("Egypt","Cairo","10,230,350")
$Null=$Table.Rows.Add("USA","Washington, D.C","658,893")
#populate datagrid
$DataView = New-Object System.Data.DataView($Table)
$array = New-Object System.Collections.ArrayList
[void] $array.AddRange($DataView)
$dg.clear()
$dg.ItemsSource = $array
$dg.IsReadOnly = $true
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
for ($i = 0; $i -lt $dg.Items.Count; $i++)
{
if ($dg.Items[$i].Row[$dg.Columns.DisplayIndex] -eq "$SearchValue")
{
[System.Windows.Forms.MessageBox]::Show("Keyword Found")
$dg.ScrollIntoView($dg.Items[$i]) #scroll to the row that contains the keyword searched
}
}
})
#Display Form
$Window.ShowDialog() | Out-Null

Using a very similar method to what you're doing I was able to get it to highlight all cells that contain the search phrase (since I misunderstood the question at first). What I had to do was iterate through columns instead of searching all columns at once for each row. That way we know what column the desired info is in, and then we can select the cell in that column after we scroll the correct row into view.
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
$dg.SelectedCells.Clear()
for ($i = 0; $i -lt $dg.Items.Count; $i++)
{
0..($dg.Columns.Count-1)|?{$dg.Items[$i].Row[$_] -eq "$SearchValue"}|%{
$dg.ScrollIntoView($dg.Items[$i],$dg.Columns[$_])
$DGCell = $dg.Columns[$_].GetCellContent($dg.Items[$i]).Parent
$DGCellInfo = New-Object System.Windows.Controls.DataGridCellInfo($DGCell)
$dg.SelectedCells.add($DGCellInfo)
}
}
})
This at least gets you the groundwork to select a cell, you'll just need to figure out how to track the current cell to be able to move on to the next one since it sounds like you want to be able to move cell to cell by clicking the Search button over and over.
Edit: In regards to setting it up to do a Find/Find Next, you could setup a global variable, set $i to that, and then set that global variable inside your loop each time. Then anytime you get a match run a break to kill the loop, and it should just pick up where it left off. Might also want to add a line to reset the global variable before the loop so that it starts over if it hits the end. Something like this should do it:
$global:SearchIndex = 0
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
$dg.SelectedCells.Clear()
for ($i = $global:SearchIndex; $i -lt $dg.Items.Count; $i++)
{
0..($dg.Columns.Count-1)|?{$dg.Items[$i].Row[$_] -eq "$SearchValue"}|%{
$dg.ScrollIntoView($dg.Items[$i],$dg.Columns[$_])
$DGCell = $dg.Columns[$_].GetCellContent($dg.Items[$i]).Parent
$DGCellInfo = New-Object System.Windows.Controls.DataGridCellInfo($DGCell)
$dg.SelectedCells.add($DGCellInfo)
$global:SearchIndex = $i
break
}
}
#check if we hit the end of the table. If we did display a notice and reset the search index.
If($global:SearchIndex -ge $dg.Items.Count){
[System.Windows.Forms.MessageBox]::Show("No more matches found, resetting search to begining of table.")
$global:SearchIndex=0
}
})
Edit2: Ok, you're right, the break stops increments, so it will just find the same result each time. In order to fix that we need to also track the column. If we change the internal loop to a For loop as well we can just increment the internal loop each time we track. We also need to track breaks, so if the inner loop breaks the outer loop will too. So, something like this should do that:
$global:SearchIndex = 0
$global:SearchIndex2 = 0
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
$dg.SelectedCells.Clear()
for ($i = $global:SearchIndex; $i -lt $dg.Items.Count; $i++)
{
$global:BreakPoint = $false
For($j=$global:SearchIndex2;$j -le 2;$j++){
If($dg.Items[$i].Row[$j] -eq "$SearchValue"){
$dg.ScrollIntoView($dg.Items[$i],$dg.Columns[$j])
$DGCell = $dg.Columns[$j].GetCellContent($dg.Items[$i]).Parent
$DGCellInfo = New-Object System.Windows.Controls.DataGridCellInfo($DGCell)
$dg.SelectedCells.add($DGCellInfo)
$global:SearchIndex = $i
$global:SearchIndex2 = $j+1
$global:BreakPoint = $true
break
}
}
If($global:BreakPoint){break}
$global:SearchIndex2=0
}
#check if we hit the end of the table. If we did display a notice and reset the search index.
If($global:SearchIndex -ge $dg.Items.Count){
[System.Windows.Forms.MessageBox]::Show("No more matches found, resetting search to begining of table.")
$global:SearchIndex=0
$global:SearchIndex2=0
}
})

Related

In Powershell, displaying a server restart check script in the textbox, freezes the WPF GUI

I created a PowerShell script that uses a WPF GUI. It has the option to choose a server from a combobox (more servers will be added later). The chosen server can be rebooted by clicking on the reboot button. Then a script, that will check the reboot procedure, will run in Button.Add_Click and the results will be shown in a textbox.
The problem is that the GUI freezes until Button.Add_Click is finished and will then show the info in the textbox.
I tried to solve this by implementing a runspace. But now I run into new problems.
In the ComboBox.add_SelectionChanged section I cannot get the selected combobox content. I want to store the content in a variable $computer.
In the Button.Add_Click section I can’t write to the textbox. When I use $Global:uiHash.Window.Dispatcher.Invoke([action]
$Global:uiHash.TextBox.AppendText("Check that reboot is initiated properlyn")},"Normal")` then the GUI freezes.
Here is the full code:
$Global:uiHash = [hashtable]::Synchronized(#{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("uiHash",$Global:uiHash)
$psCmd = [PowerShell]::Create().AddScript({
$Global:uiHash.Error = $Error
Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase
$xaml = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="CSS Server Reboot" Height="450" Width="800">
<Grid>
<ComboBox Name="Server_Combobox" HorizontalAlignment="Left" Margin="240,107,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem Name="Server1">10.15.12.148</ComboBoxItem>
</ComboBox>
<Label Name="Title_Label" Content="CSS – Console Quentris, Server Reboot
" HorizontalAlignment="Left" Margin="240,41,0,0" VerticalAlignment="Top" Height="34" Width="284" FontSize="16"/>
<Button Name="Reboot_Button" Content="Reboot" HorizontalAlignment="Left" Margin="449,107,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox Name="Reboot_Textbox" Grid.Column="1" HorizontalAlignment="Left" Height="173" Margin="81,180,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="294"/>
</Grid>
"#
$Global:uiHash.Window=[Windows.Markup.XamlReader]::Parse($xaml )
$Global:uiHash.TextBox = $Global:uiHash.window.FindName("Reboot_Textbox")
$Global:uiHash.Button = $Global:uiHash.window.FindName("Reboot_Button")
$Global:uiHash.ComboBox = $Global:uiHash.window.FindName("Server_Combobox")
$Global:uiHash.Window.ShowDialog() | out-null
})
$psCmd.Runspace = $newRunspace
$handle = $psCmd.BeginInvoke()
Start-Sleep -Milliseconds 100
$computer = ""
$Global:uiHash.ComboBox.add_SelectionChanged({
$Script:computer = $Global:uiHash.Combobox.SelectedItem.Content
})
$Global:uiHash.Button.Add_Click({
$Username = 'xxxx'
$Password = 'xxxx'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$SecureString = $pass
$MySecureCreds = New-Object -TypeName
System.Management.Automation.PSCredential -ArgumentList
$Username,$SecureString
Restart-Computer -ComputerName $computer -Force -Credential $MySecureCreds
$timeout=5
$MAX_PINGTIME = $timeout * 60
$max_iterations = $MAX_PINGTIME/5
$Notification_timeout = 10 # in seconds
function ping-host {
param($pc)
$status = Get-WmiObject -Class Win32_PingStatus -Filter "Address='$pc'"
if( $status.statuscode -eq 0) {
return 1
} else {
return 0
}
}
if(ping-host -pc $computer) {
$status = "online`n"
for ($i=0; $i -le $max_iterations; $i++) {
if (!(ping-host -pc $computer )) {
break
}
Start-Sleep -Seconds 5
if($i -eq $max_iterations) {
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("$computer never went down in last $timeout minutesn")},"Normal")
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("Check that reboot is initiated properlyn")},"Normal")
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("$computer is still ONLINE; Check that reboot is initiated properly`n")},"Normal")
exit
}
}
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("$computer is offline now; monitoring for online status`n")},"Normal")
} else {
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("$computer is offline; Monitoring for online statusn")},"Normal")
$status = "offlinen"
}
for ($i=0; $i -le $max_iterations; $i++) {
if ((ping-host -pc $computer )) {
break
}
Start-Sleep -Seconds 5
if($i -eq $max_iterations) {
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("Your computer never came back online in last $MAX_PINGTIME secondsn")},"Normal")
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("Check that nothing is preventing starupn")},"Normal")
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText("$Computer is NOT coming online; Something is preventing its startup`n")},"Normal")
exit
}
}
$Global:uiHash.Window.Dispatcher.Invoke([action]
{$Global:uiHash.TextBox.AppendText("Your computer is Online Now; Task done;
exiting")},"Normal")
})
The reason that the GUI freeze is that the job you are running is in the same instance.
Since the GUI runs in the same instance it will have to wait until whatever code is running is done before it can go back to taking new input.
You could try and solve this running the code you want to execute when you click the button as a job, this way the code will run in a new process on the machine and not block the session the GUI is running in.
This can be done with the Start-Job cmdlet

Windows Forms: Cell Wrapping mode to display adaptive ellipsis

I'm using a Windows Forms Datagridview to diplay some (long) text. (The code is PowerShell, but the problem is related to the Cell Wrapping mode)
$TestGridView = New-Object System.Windows.Forms.DataGridView -Property #{
Name="TestDataGridView"
AllowUserToAddRows = $False
AllowUserToDeleteRows = $False
Location = "14,225"
Size = "1041,328"
TabIndex = 1
DefaultCellStyle= #{WrapMode ='True'}
RowHeadersVisible=$False
AutoSizeColumnsMode='Fill'
AutoSizeRowsMode = 'AllCells'
Anchor = 'Left, Right, Top, Bottom'
DefaultCellStyle.Padding = new-object Windows.Forms.Padding -a 2
}
I'm using Cell Wrapping and AutosizeRowMode, but I have found no way to have a DGV cell display up to a certain point, then truncate by ellipsis when the cell size is exceeded.
What I'd want to accomplish is this: (graphic edit)
but so far, I've been unable to do so:
WrapMode=False,AutoSizeRowsMode=AllCells
truncates by ellipsis, but removes all CRLFs and displays just one line
WrapMode=False,AutoSizeRowsMode=None
Row height set to desired value, but otherwise truncation same as above
WrapMode=True,AutoSizeRowsMode=AllCells
No truncation, all the text is displayed and the cell is adapted in height to fit all the text
WrapMode=True,AutoSizeRowsMode=None
Height stays as it shoulds, but no truncation is performed.
What I'm trying to accomplish is to have the rows adjust in size up to a maximum, after which text should be truncated by ellipsis [...]
I'have already tried truncating the content, but it has the adverse side effect that when the user COPY the cell content, the cell content is missing all the truncated part (of course) so it is not a viable option..
Many thanks
You need to handle CellPainting event yourself and draw text yourself by applying word-wrapping and ellipsis:
Function dgv_CellPainting{[CmdletBinding()]param(
[parameter()]
[Object]$sender,
[parameter()]
[System.Windows.Forms.DataGridViewCellPaintingEventArgs]$e
)
#Don't process if it's not the column which we want or it's a header row
if (($e.ColumnIndex -ne 0) -or ($e.RowIndex -lt 0)){ return }
#Paint all parts but text
$e.Paint($e.CellBounds, [System.Windows.Forms.DataGridViewPaintParts]::All `
-band (-bnot([System.Windows.Forms.DataGridViewPaintParts]::ContentForeground)))
$color = $e.CellStyle.ForeColor
if ($sender.Rows[$e.RowIndex].Cells[$e.ColumnIndex].Selected -eq $true){
$color = $e.CellStyle.SelectionForeColor}
#Paint text
[System.Windows.Forms.TextRenderer]::DrawText($e.Graphics, $e.FormattedValue, `
$e.CellStyle.Font, $e.CellBounds, $color, `
[System.Windows.Forms.TextFormatFlags]::VerticalCenter -bor `
[System.Windows.Forms.TextFormatFlags]::TextBoxControl -bor `
[System.Windows.Forms.TextFormatFlags]::WordBreak -bor `
[System.Windows.Forms.TextFormatFlags]::EndEllipsis)
#Event handled, stop default processing
$e.Handled = $true
}
Full Example
Here is a full working PowerShell example. To see the effect, you can try to resize the column or row.
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Add_Load({form_Load -sender $form -e $_})
$dgv = New-Object System.Windows.Forms.DataGridView
$dgv.Dock = [System.Windows.Forms.DockStyle]::Fill
$dgv.RowTemplate.Height = 50
$dgv.Add_CellPainting({dgv_CellPainting -sender $dgv -e $_})
$form.Controls.Add($dgv)
Function form_Load {[CmdletBinding()]param(
[parameter()]
[Object]$sender,
[parameter()]
[System.EventArgs]$e
)
$dt = New-Object System.Data.DataTable
$dt.Columns.Add("Column1")
$dt.Rows.Add("Lorem ipsum dolor sit amet, " + `
"wisi fierent fabellas pri et, eum aeterno volumus no.")
$dgv.DataSource = $dt
#Enable multiline editing
$dgv.Columns[0].DefaultCellStyle.WrapMode = `
[System.Windows.Forms.DataGridViewTriState]::True
}
Function dgv_CellPainting{[CmdletBinding()]param(
[parameter()]
[Object]$sender,
[parameter()]
[System.Windows.Forms.DataGridViewCellPaintingEventArgs]$e
)
#Don't process if it's not the column which we want or it's a header row
if (($e.ColumnIndex -ne 0) -or ($e.RowIndex -lt 0)){ return }
#Paint all parts but text
$e.Paint($e.CellBounds, [System.Windows.Forms.DataGridViewPaintParts]::All `
-band (-bnot([System.Windows.Forms.DataGridViewPaintParts]::ContentForeground)))
$color = $e.CellStyle.ForeColor
if ($sender.Rows[$e.RowIndex].Cells[$e.ColumnIndex].Selected -eq $true){
$color = $e.CellStyle.SelectionForeColor}
#Paint text
[System.Windows.Forms.TextRenderer]::DrawText($e.Graphics, $e.FormattedValue, `
$e.CellStyle.Font, $e.CellBounds, $color, `
[System.Windows.Forms.TextFormatFlags]::VerticalCenter -bor `
[System.Windows.Forms.TextFormatFlags]::TextBoxControl -bor `
[System.Windows.Forms.TextFormatFlags]::WordBreak -bor `
[System.Windows.Forms.TextFormatFlags]::EndEllipsis)
#Event handled, stop default processing
$e.Handled = $true
}
$form.ShowDialog()
$form.Dispose()

Listbox Bound to a PSObject list DisplayMember not working

I have a form with a listbox on it. In this listbox I want to put PSObjects that have these members: Name, Location, ID.
I have created a variable: $list that holds all the PSObjects and I want to bind $list to the $listbox so that anytime $list is modified the $listbox automatically updates.
I am able to bind $list to the $listbox but it doesn't display it properly. I would like only the name member to be visible. The problem is, regardless of if I set the DisplayMember property or not, the $list is still displayed like this:
_______________________________________
|#{Name=Jim; Location=Somewhere; ID=0}|
|#{Name=Sam; Location=Somewhere; ID=1}|
|_____________________________________|
I want it to look like this:
______
|Jim |
|Sam |
|____|
Here's the relevant code I'm working with:
#####PSObject Initialization Code:##########
$obj1 = New-Object -TypeName PSObject -Property #{
'Name' = 'Jim';'Location' = 'Somewhere';'ID' = 0
};$obj2 = New-Object -TypeName PSObject -Property #{
'Name' = 'Sam';'Location' = 'Somewhere';'ID' = 1
};$list = $obj1,$obj2
#####Listbox Code:##########################
$listbox = New-Object System.Windows.Forms.ListBox
$listbox.Dock = 'Fill'
$listbox.SelectionMode = 'MultiExtended'
$listbox.DisplayMember = 'Name'
$listbox.DataSource = $list
#####Form Code##############################
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(500,500)
$form.Controls.Add($listbox); $form.ShowDialog()
Does anyone have an idea about why the DisplayMember property is seemingly overlooked when I bind the list and how to get the display into the desired format?
Note: If the PSObjects are added from the list one at a time, then it displays properly, but this gets clunky in my application.
Figured I would post this even if it's old, a bunch of folk have looked
The trick was to cast the array as an arraylist:
$listbox.DataSource = [system.collections.arraylist]$itemList

WPF runspace crashes using powershell version 2

I'm writing a powershell script, which installes various drivers.
My script is working fine, so I want to add a gui using WPF.
First I created a gui using WPF, nothing spectacular, just a window with a label.
I would like to update this label from my install script. So I create two runspaces, one that creates and shows the WPF gui and the other one executes my install script.
That works fine as long as I'm using powershell version 3 or higher. With powershell 2, which I had to use with a new Windows 7 installation, the wpf runspace crashes down.
I hope there is a way to get this working with powershell version 2.
Here is a sample script, demonstating what I'm doing.
#########################################################################################
#
# W P F - R U N S P A C E
#
#########################################################################################
$syncHashWpfLuaNotification = [hashtable]::Synchronized(#{})
$runspaceWpfLuaNotification =[runspacefactory]::CreateRunspace()
$runspaceWpfLuaNotification.ApartmentState = "STA"
$runspaceWpfLuaNotification.ThreadOptions = "ReuseThread"
$runspaceWpfLuaNotification.Open()
$runspaceWpfLuaNotification.SessionStateProxy.SetVariable("syncHashWpfLuaNotification",$syncHashWpfLuaNotification)
$psCmd = [PowerShell]::Create().AddScript({
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$xaml = #"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TreiberInstaller" Height="431" Width="626" Background="Black"
WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Label Name="lblProgress" Content="Progress" HorizontalAlignment="Center" Grid.Column="1" Grid.Row="2" VerticalAlignment="Top" Foreground="White" FontFamily="Calibri" FontSize="18" HorizontalContentAlignment="Center"/>
</Grid>
</Window>
"#
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHashWpfLuaNotification.Window = [Windows.Markup.XamlReader]::Load( $reader )
$syncHashWpfLuaNotification.lblProgress = $syncHashWpfLuaNotification.window.FindName("lblProgress")
$syncHashWpfLuaNotification.Window.ShowDialog() | Out-Null
$syncHashWpfLuaNotification.Error = $Error
})
$psCmd.Runspace = $runspaceWpfLuaNotification
$data = $psCmd.BeginInvoke()
Sleep -Milliseconds 450 # Wait a moment that the gui is ready
#########################################################################################
#
# W O R K E R - R U N S P A C E
#
#########################################################################################
$ScriptBlock = {
#----------------------------------------------------------------------
# SetLabelText: Sets the lable-text
#----------------------------------------------------------------------
function SetLabelText
{
param(
[Parameter(Position=0, Mandatory = $true, ValueFromPipeline = $false)]
[ValidateNotNullOrEmpty()]
[string]$Text
)
if(-not $syncHashWpfLuaNotification) {return}
try
{
$syncHashWpfLuaNotification.Window.Dispatcher.invoke(
[action]{$syncHashWpfLuaNotification.lblProgress.Content = $Text},
"Normal"
)
}
catch {}
}
#----------------------------------------------------------------------
# CloseProgressWindow: Closes the window
#----------------------------------------------------------------------
function CloseProgressWindow()
{
if(-not $syncHashWpfLuaNotification) {return}
try
{
$syncHashWpfLuaNotification.Window.Dispatcher.invoke(
[action]{$syncHashWpfLuaNotification.Window.Close()},
"Normal"
)
}
catch{}
}
#Starting here
SetLabelText -Text "Starting installation..."
Sleep 2
for($i=1;$i -le 19; $i++)
{
SetLabelText -Text ("Progress Step " + $i)
}
for($i=20;$i -le 24; $i++)
{
SetLabelText -Text ("Progress Step " + $i)
Sleep 1
}
CloseProgressWindow
} #End of $ScriptBlock
$syncHash1 = [hashtable]::Synchronized(#{})
$workerRunspace =[runspacefactory]::CreateRunspace()
$workerRunspace.ApartmentState = "STA"
$workerRunspace.ThreadOptions = "ReuseThread"
$workerRunspace.Open()
$workerRunspace.SessionStateProxy.SetVariable("syncHash1",$syncHash1)
$workerRunspace.SessionStateProxy.SetVariable("syncHashWpfLuaNotification",$syncHashWpfLuaNotification)
$psCmd1 = [PowerShell]::Create().AddScript($ScriptBlock)
$psCmd1.Runspace = $workerRunspace
$data = $psCmd1.BeginInvoke()
#########################################################################################
#
# S C R I P T E N D
#
#########################################################################################
#Wait for end of both runspaces
while(($runspaceWpfLuaNotification.RunspaceAvailability -eq "Busy") -or ($workerRunspace.RunspaceAvailability -eq "Busy"))
{
if($runspaceWpfLuaNotification.RunspaceAvailability -eq "Busy") { Write-Host "Window is open" }
if($workerRunspace.RunspaceAvailability -eq "Busy") { Write-Host "Worker is running" }
Sleep 1
}
Write-Host "Script ended"
There are some problems with how your pass delegates to Dispatcher.Invoke call.
ScriptBlock literals are bounded to current session state. If you pass bounded ScriptBlock to different Runspace, then it can cause some undesired effects, like code not invoked in target Runspace or deadlocks. See my other answer about this. So, first thing you need to do is to create new not bounded ScriptBlock. You can do that with [ScriptBlock]::Create method.
Since ScriptBlock is no more bounded to current session state, you can not refer variables from it, like you do with $Text in your code. You should pass them as additional parameters to Dispatcher.Invoke.
In .NET Framework 3.5, which will be used with PowerShell v2, there is no Invoke(Action, DispatcherPriority) overload in Dispatcher class, so that, Invoke([Action]{...}, "Normal") will be resolved to this Invoke(Delegate, Object[]) method instead. You should use different overload: Invoke(DispatcherPriority, Delegate), which exists in .NET Framework 3.5.
Try downloading WPFRunspace, which should work with PS V2. It provides a background worker for WPF and Forms based scripts.

Powershell: Creating Custom DataGridView

So I'm trying to create a custom Datagridview to put into a GUI program I've made but I'm having some trouble.
So far I have this:
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(800,400)
$form.Controls.Add($dataGridView)
$dataGridView.ColumnCount = 4
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns[0].Name = "Process"
$dataGridView.Columns[1].Name = "ID"
$dataGridView.Columns[2].Name = "Description"
$dataGridView.Columns[3].Name = "Memory"
$row1 = get-process -property name | select Name
$rows = #($row1)
foreach ($row in $rows)
{
$dataGridView.Rows.Add($row.name)}
$form.ShowDialog()
My question is this:
How do I go about assigning different columns to differnt properties, so column 'process' would be for the procress name, column 'id' would be for the process id and so on.
so far, all I've managed to do is to assign one column a input range: Process Name.
Please help!
Thanks
Loop on all processes, and add each process properties in the order you have defined the columns:
get-process | foreach{
$dataGridView.Rows.Add($_.Name,$_.ID,$_.Description,$_.WorkingSet)
}
You could also generate the columns dynamically by selecting the properties you want to display, each property becomes a column name, and use the grid's DataSource property and an Array list to add the objects to the grid:
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)
$gps = get-process | select Name,ID,Description,#{n='Memory';e={$_.WorkingSet}}
$list = New-Object System.collections.ArrayList
$list.AddRange($gps)
$dataGridView = New-Object System.Windows.Forms.DataGridView -Property #{
Size=New-Object System.Drawing.Size(800,400)
ColumnHeadersVisible = $true
DataSource = $list
}
$form.Controls.Add($dataGridView)
$form.ShowDialog()
But why no to user Out-GridView from PoSH?:
get-process | select name, process, id, description, workingSet | Out-GridView
Akim - scripters try one-liners with output you can see but do little with, while programmers think about a user interface and putting control of the output in the user's hands, thus System.Windows.Forms.Form is a powershell programmer's best friend.
Shay - I have been doing some programming to use a DataGridView and my experience so far shows me I must take control of defining the DataGridView properties and NOT use .DataSource as shown in one example above. Convenient as it is, you do not then do much with your DataGridView other than show it on the form.
I started by setting .ColCount to the number of columns I wanted. Then named the columns. Thereafter I can tweak each column's properties by numbered location or name. I chose to let the user SORT on selected columns. As of 2013-03-23 I am still working on how to set the backgroundcolor of cells that I want to highlight. Should have that answer soon for those wanting to do the same. One example I found uses the value in the cell, not the location.
$datagridview = New-Object System.Windows.Forms.DataGridView
$datagridview.ColumnCount = 8
$datagridview.Columns[0].Name = "#ID"
$datagridview.Columns[1].Name = "Name"
...[snip]...
$datagridview.Columns[7].Name = "Company"
$datagridview.Columns["Name"].SortMode = "Automatic"
$datagridview.Columns[8].SortMode = "Automatic"
$datagridview.Columns[0].Width = 50
$datagridview.Columns["Description"].Width = 350
...[snip]...
foreach ($_ in $arraylist){[void]$datagridview.Rows.Add($($_.ID), $($_.Name),$($_.Path), $($_.Description), $($_.VM), $($_.WS), $($_.CPU), $($_.Company))}
I tried several ways to fiddle with cell backgroundcolors and only had success with Add_CellPainting. Heed the MSDN warning to NOT set the cellstyle in a specific location unless you truly want that location changed no matter what the user does. In the code below, row 2 column 4 is red no matter how you sort the datagridview. That could be an OOPS or did you really want that. Hmmm.... setting by value does all matching values, so if you have non-unique values then maybe you need additional logic to change only the one you want and repaint if the contents change.
$datagridview.Add_CellPainting(
{
param($Null, $EventArgs)
if ($([String]$EventArgs.Value).ToLower().Contains("ms") -eq $True) { $EventArgs.CellStyle.BackColor = "Blue" ; $EventArgs.CellStyle.ForeColor = "White" }
if ($([String]$EventArgs.Value).ToLower().Contains("windows") -eq $True) { $EventArgs.CellStyle.BackColor = "Yellow" }
if ($([String]$EventArgs.Value).ToLower().Contains("windows powershell") -eq $True) { $EventArgs.CellStyle.BackColor = "Green" }
if (($EventArgs.RowIndex -eq 2) -and ($EventArgs.ColumnIndex -eq 4)) {$EventArgs.CellStyle.BackColor = "Red" }
}
) # End of Add_CellPainting
Have since found another way to highlight the cell of my choosing:
$Script:PS_Log_Viewer_Form_row = $PS_Log_Viewer_Form_dataGridView1.Rows.Add("$($PS_Log_Viewer_total_records_ctr)", "$($PS_Log_Viewer_Form_line_date_time_sub)","$($PS_Log_Viewer_Form_line_acct)","$($PS_Log_Viewer_Form_line_msg)", "$($PS_Log_Viewer_Form_full_filename)-$($PS_Log_Viewer_file_records)")
$PS_Log_Viewer_Form_dataGridView1.Rows[$PS_Log_Viewer_Form_row].Cells[1].Style.BackColor = "BlanchedAlmond"
And searching all rows and setting (think find all):
for ($i = ($PS_Log_Viewer_Form_dataGridView1.FirstDisplayedScrollingRowIndex - 1) ; $i -gt 0 ; $i-- )
{
if ($PS_Log_Viewer_Form_dataGridView1.Rows[$i].Cells[3].Value.Contains("$($find_form_middle_flp_textbox_1.Text)") )
{
$PS_Log_Viewer_Form_dataGridView1.Rows[$i].Cells[3].Style.BackColor = $find_form_middle_flp_color_combobox_dropdownlist.SelectedItem
$PS_Log_Viewer_Form_dataGridView1.FirstDisplayedScrollingRowIndex = $i
$find_form_bottom_remarks = "Previous found at $($i)."
$i = 0
} # End of if ($PS_Log_Viewer_Form_dataGridView1.Rows[$i].Cells[3].Value.Contains("$($Script:PS_Log_Viewer_search_string)") )
} # End of for ($i = 0 ; $i -lt $PS_Log_Viewer_Form_dataGridView1.RowCount ; $i++ )

Resources