如何使用Powershell合并DataGridView中的单元格

时间:2015-02-19 18:54:44

标签: winforms user-interface powershell datagridview

亲爱的Powershell学者, 我最近发现使用WinForms和PowerShell是创建小而实用的GUI的好方法。我目前正在使用DataGridView,并试图想象视觉属性 - 具体来说,我想知道是否有一种方法来合并单元格,以便我可以使'子标题'反映我的树中的哪个父节点与选中的孩子。如果这没有多大意义,我想要完成的内容如下所示:

enter image description here

有没有办法合并和居中细胞或至少一种方法去除某些细胞上的网格线,以实现描绘的鲑鱼色行效果?

虽然PowerShell解决方案不易翻译,但此person和此other person提出了类似的问题。

不遵循树功能的基本代码(改编自technet post):

#The following is adapted from https://gallery.technet.microsoft.com/ScriptCenter/3dcf0354-e7a7-482c-86f1-2e75809a502d/

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://www.ravichaganti.com/blog) 
######################################################################## 

#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

这可能吗?

1 个答案:

答案 0 :(得分:0)

This介绍了如何为这两个事件实施&#34;处理程序,以便提供渐变选择背景和跨越多列的某些自定义前景内容。&#34; < / p>

要回答这个问题,我浏览了文档here。 Microsoft文档并不总是深入探讨如何使用API​​,但是当它发生时,它值得一读,就像在这种情况下一样。