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()
Related
I have read an input CSV file and generated a chart in PowerShell form.
This is my code:
$datasource = Import-Csv "D:\Users\janaja\Desktop\test.csv"
[void] [Reflection.Assembly]::loadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::loadWithPartialName('System.Windows.Forms.DataVisualization')
$datasource = "D:\Users\janaja\Desktop\test.csv"
$outputXLSX = "D:\Users\janaja\Desktop\test.xlsx"
$excel = New-Object -ComObject excel.application
$xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$TxtConnector = ("TEXT;" + $datasource)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
$query.AdjustColumnWidth = 1
$query.Refresh()
$query.Delete()
$Workbook.SaveAs($outputXLSX,51)
$i = 1
$j=1
while($j -le 20){
$excel.Cells.Item($i, $j).Font.ColorIndex = 3
$excel.Cells.Item($i, $j).Font.Bold = $True
$j++
}
$Chart = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '600,750'
$ChartArea = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'VM Name'
$ChartArea.AxisY.Title = 'CPU and Memory Utilization'
$ChartArea.AxisX.lnterval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('Memory')
$Chart.Series.Add('CPU')
$Chart.Series['Memory'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
$Chart.Series['CPU'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
Foreach($VM in $datasource)
{
$var1 = $Chart.Series['Memory'].Points.AddXY($VM.VMName,$VM.MemoryGB)
}
Foreach($CPU in $datasource)
{
$var2 = $Chart.Series['CPU'].Points.AddXY($VM.VMName,$VM.CPU)
}
$Title = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[O].Text = 'VM details'
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
[System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor
[System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Text= "VM Memory and CPU usage Chart"
$Form.Width = 600
$Form.Height = 600
$Form.controls.add($Chart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Chart.Savelmage($Env:USERPROFILE + "\Desktop\Chart.png", "PNG")
This code converts the file from CSV to Excel format and then reads the CSV file data to display a chart on the Powershell Form.
How do I Generate the same chart which takes only 3 column values out of 14 columns and display a clustered bar chart?
You cannot embed pictures in CSV files the way you probably intend, because CSV is a plaintext format. You could embed a path to a picture file, or you could encode the picture (e.g. using base64 encoding) and include the encoded data. But even with the latter you'd still need a viewer that would decode the encoded data and render them as a picture.
Essentially, what you're asking is not possible.
However, you could use the ImportExcel to create an aribtrary Excel spreadsheet from an existing CSV file. The module supports creating Excel charts as well, which should be able to reproduce a similar chart to the one you're creating with your image. You may also be able to just embed the image file, but I'm not sure if that's possible. You may need to use the much older Excel COM API that ships with Office to do that.
How can I achieve that the input of the TextBox with multilines is only possible to a specific number of lines e.g. 10 lines only.
Further I want to get the input of each line and write each line to a separate variable to work later with this variables. It would be nice if the user gets a messagebox with warning that only 10 lines are possible.
Any help would be appreciated
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#Assembly PresentationFramework wird geladen
Add-Type -AssemblyName PresentationFramework
$form = New-Object System.Windows.Forms.Form
$form.StartPosition = 'CenterScreen' #Formstartposition Zentrum
$form.Size = New-Object System.Drawing.Size(500,400)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.DataBindings.DefaultDataSourceUpdateMode = 0
$textBox.Location = New-Object System.Drawing.Point((110),(90))
$textBox.Size = New-Object System.Drawing.Size(288,150)
$textBox.TabIndex = 0
$textBox.Multiline =$true
$form.Controls.Add($textBox)
$form.ShowDialog() | Out-Null
1.Get Line count on the event of TextChanged.
2.Turn the iList into a ArrayList.
3.Get the difference from max line to current line count.
4.Remove the range from the ArrayList.
5.Set the content of the Textbox to the Arraylist
6.Set the curser to end of textbox.
$TextboxMaxLines = 10
$textBox.Add_TextChanged({
If($textBox.Lines.Count -gt $TextboxMaxLines){
[System.Collections.ArrayList]$AL = $textBox.Lines
[int]$LC = ($textBox.Lines.Count - $TextboxMaxLines)
$Al.RemoveRange($TextboxMaxLines, $LC)
$textbox.Lines = $AL
$textbox.SelectionStart = ($textbox.Text.Length)
$textbox.SelectionLength = 0
}
})
I have a script I need to use for multiple parameter data collection, as follows:
function Build-FormPanel($FormTitle){
Add-Type -Assembly System.Windows.Forms ## Load the Windows Forms assembly
## Create the main form
$form = New-Object Windows.Forms.Form
$form.FormBorderStyle = "FixedToolWindow"
$form.Text = $FormTitle
$form.AutoScroll = $True
$form.StartPosition = "CenterScreen"
$form.Width = 740 ; $form.Height = 480 # Make the form wider
#Add Buttons- ## Create the button panel to hold the OK and Cancel buttons
$buttonPanel = New-Object Windows.Forms.Panel
$buttonPanel.Size = New-Object Drawing.Size #(400,40)
$buttonPanel.Dock = "Bottom"
$cancelButton = New-Object Windows.Forms.Button
$cancelButton.Top = $buttonPanel.Height - $cancelButton.Height - 10; $cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
$cancelButton.Text = "Cancel"
$cancelButton.DialogResult = "Cancel"
$cancelButton.Anchor = "Right"
## Create the OK button, which will anchor to the left of Cancel
$okButton = New-Object Windows.Forms.Button
$okButton.Top = $cancelButton.Top ; $okButton.Left = $cancelButton.Left - $okButton.Width - 5
$okButton.Text = "Ok"
$okButton.DialogResult = "Ok"
$okButton.Anchor = "Right"
## Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
## Add the button panel to the form
$form.Controls.Add($buttonPanel)
## Set Default actions for the buttons
$form.AcceptButton = $okButton # ENTER = Ok
$form.CancelButton = $cancelButton # ESCAPE = Cancel
return $form
}
$LeftMargin = 25
$BottomMargin = 30
$i = 0
$form = Build-FormPanel "Please update server configurations"
foreach($param in $hash){#Where $hash is an "dictionary" of key/value pairs
$k = $param.Key
$v = $param.Value
$lblValue = New-Object System.Windows.Forms.Label
$lblValue.Text = $k+":"
$lblValue.Top = 20*$i ; $lblValue.Left = $LeftMargin; $lblValue.Width=150 ;$lblValue.AutoSize = $true
$form.Controls.Add($lblValue) # Add to Form
#
$txtValue = New-Object Windows.Forms.TextBox
$txtValue.Top = 20*$i; $txtValue.Left = 160; $txtValue.Width = 320;
$txtValue.Text = $v
$form.Controls.Add($txtValue) # Add to Form
$i++
}
$form.Topmost = $True
$form.Add_Shown( { $form.Activate(); } )
$result = $form.ShowDialog()
if($result -eq "OK")
{
$j = 0;
foreach($param in $hash){
${"txtValue_$j"}.Text
$j++
}
}
else {Write-Host "Cancel"}
Basically, this works OK to display the form and inputs. But after submission, I am unable to capture all the user inputs. Only the last input value is captured, obviously because the variables get overwritten in the loop.
How can I achieve capturing the data as described?
The issue as you have mentioned is because of the its getting overwritten.
I can give you a logical set off.
YOu can use it in a loop and in the loop , you store all the data either in array or if its dynamic then you can use arraylist by using
New-Object System.Collections.ArrayList
. But recommended is to create PSCustomObject , store in that and add that to the arraylist each time.
Finally you can get the output captured in the arraylist.
Further you can try making the arraylist Global so that it will be available for the entire script.
Hope it helps.
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++ )
So far I have this code.
$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)
$go = New-Object System.Windows.Forms.Button
$go.Location = New-Object System.Drawing.Size(300,450)
$go.Size = New-Object System.Drawing.Size(75,23)
$go.text = "Select"
$form.Controls.Add($go)
$form.Controls.Add($dataGridView)
$dataGridView.ColumnCount = 4
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns[0].Name = "Name"
$dataGridView.Columns[1].Name = "ID"
$dataGridView.Columns[2].Name = "Description"
$dataGridView.Columns[3].Name = "Memory"
$dataGridView.Columns[0].width = 240
get-process | foreach {
$dataGridView.Rows.Add($_.Name,$_.ID,$_.Description,$_.WorkingSet) | out-null
}
$go.Add_Click({
$selectedRow = $dataGridView.CurrentRowIndex
write-host $selectedRow
})
[void]$form.ShowDialog()
It simply gets the Process Name, ID, etc. properties and puts them into pre-defined headers in a DataGridView.
My problem is that I want to see the row I've clicked on via $selectedRow = $dataGridView.CurrentRowIndex and output it to the console. Instead, when the 'Select' button is pushed, a blank string is output to the terminal.
You can also get the row index with:
$dataGridView.CurrentCell.RowIndex
or
$dataGridView.SelectedRows[0].Index
You may also want to set the grid MultiSelect property to $false. Currently it allows multiple rows selection. Another thing to consider is setting the SelectionMode property to 'FullRowSelect'. When the grid is populated the first column is selected, not the whole row.
Change
$selectedRow = $dataGridView.CurrentRowIndex
to
$selectedRow = $dataGridView.CurrentRow.Index