mardi 24 février 2015

How to merge cells in DataGridView using Powershell

Dear Powershell savants, I recently discovered that using WinForms with PowerShell is a great way to create small yet really useful GUIs. I'm currently working with DataGridView and am trying to get fancy with visual properties - specifically, I was wondering if there was a way to merge cells so that I could make 'sub-headers' reflecting which parent node from my tree is associated with selected children. If that doesn't make much sense, what I'm trying to accomplish is depicted in the following:


enter image description here


Is there a way to merge and center cells or at least a way to remove grid lines on certain cells to achieve the depicted salmon colored row effect?


This person and this other person asked similar questions although a PowerShell solution isn't readily translated.


Base code without tree functionality follows (adapted from technet post):



#The following is adapted from http://ift.tt/1G8Ejlb

function Get-ProcessInfo {
$array = New-Object System.Collections.ArrayList
$Script:procInfo = Get-Process | Select Id,Name,Path,Description | sort -Property Name
$array.AddRange($procInfo)
$dataGridView1.DataSource = $array
$form1.refresh()
}

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 2/24/2010 11:38 AM
# Generated By: Ravikanth Chaganti (http://ift.tt/POdePY)
########################################################################

#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
$label1 = New-Object System.Windows.Forms.Label
$button3 = New-Object System.Windows.Forms.Button
$button2 = New-Object System.Windows.Forms.Button
$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.
$button3_OnClick=
{
#$Form1.Close()
#$dataGridView1.SelectedRows | Foreach {$dataGridView1.Rows[$_.Index].DefaultCellStyle.BackColor = "Red"}
# $System_Windows_Forms_DataGridViewCellStyle_1.BackColor = 'White'
# $dataGridView1.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1

#Wipe Highlights without refreshing form
for($i=0; $i -le $dataGridView1.RowCount-1; $i++) {$dataGridView1.Rows[$i].DefaultCellStyle.BackColor = "White"}


}

$button1_OnClick=
{
#Get-ProcessInfo
}

$button2_OnClick=
{

$dataGridView1.SelectedRows | Foreach {$dataGridView1.Rows[$_.Index].DefaultCellStyle.BackColor = "Red"}


# $selectedRow = $dataGridView1.SelectedRows[0].Index
# if (($procid=$Script:procInfo[$selectedRow].Id)) {
#Stop-Process -Id $procid -Confirm
# $dataGridView1.Rows.Item($selectedRow).DefaultCellStyle.BackColor = "Red"

#}
}

$OnLoadForm_UpdateGrid=
{
Get-ProcessInfo
}

#----------------------------------------------
#region Generated Form Code
$form1.Text = "Primal Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 517
$System_Drawing_Size.Height = 414
$form1.ClientSize = $System_Drawing_Size

$label1.TabIndex = 4
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 155
$System_Drawing_Size.Height = 23
$label1.Size = $System_Drawing_Size
$label1.Text = "Process Manager"
$label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,2,3,0)
$label1.ForeColor = [System.Drawing.Color]::FromArgb(255,0,102,204)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 13
$label1.Location = $System_Drawing_Point
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Name = "label1"

$form1.Controls.Add($label1)

$button3.TabIndex = 3
$button3.Name = "button3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button3.Size = $System_Drawing_Size
$button3.UseVisualStyleBackColor = $True

$button3.Text = "Clear Highlights"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 429
$System_Drawing_Point.Y = 378
$button3.Location = $System_Drawing_Point
$button3.DataBindings.DefaultDataSourceUpdateMode = 0
$button3.add_Click($button3_OnClick)

$form1.Controls.Add($button3)

$button2.TabIndex = 2
$button2.Name = "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button2.Size = $System_Drawing_Size
$button2.UseVisualStyleBackColor = $True

$button2.Text = "Highlight"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 230
$System_Drawing_Point.Y = 378
$button2.Location = $System_Drawing_Point
$button2.DataBindings.DefaultDataSourceUpdateMode = 0
$button2.add_Click($button2_OnClick)

$form1.Controls.Add($button2)

$button1.TabIndex = 1
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True

$button1.Text = "Refresh"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 379
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 492
$System_Drawing_Size.Height = 308
$dataGridView1.Size = $System_Drawing_Size
$dataGridView1.DataBindings.DefaultDataSourceUpdateMode = 0
$dataGridView1.Name = "dataGridView1"
$dataGridView1.DataMember = ""
$dataGridView1.TabIndex = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 48
$dataGridView1.Location = $System_Drawing_Point

$form1.Controls.Add($dataGridView1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState

#Add Form event
$form1.add_Load($OnLoadForm_UpdateGrid)

#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm


Is this possible?


Aucun commentaire:

Enregistrer un commentaire