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

标签 winforms user-interface powershell datagridview

亲爱的 Powershell 专家, 我最近发现,将 WinForms 与 PowerShell 结合使用是创建小型但真正有用的 GUI 的好方法。我目前正在使用 DataGridView 并尝试着迷于视觉属性 - 具体来说,我想知道是否有一种方法可以合并单元格,以便我可以制作“子标题”来反射(reflect)与我的树中的哪个父节点相关联选定的 child 。如果这没有多大意义,我想要完成的任务如下所示:

enter image description here

有没有办法合并和居中单元格,或者至少有办法删除某些单元格上的网格线以实现所描绘的鲑鱼色行效果?

这个person还有这个other person尽管 PowerShell 解决方案不容易翻译,但也提出了类似的问题。

不带树功能的基本代码如下(改编自 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

这可能吗?

最佳答案

This描述如何实现“这两个事件的处理程序,以提供渐变选择背景和一些跨多列的自定义前景内容。”

为了回答这个问题,我浏览了文档 here 。 Microsoft 文档并不总是深入探讨如何使用 API,但当深入探讨时,就值得一读,就像本例中的情况一样。

关于winforms - 如何使用 Powershell 合并 DataGridView 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28614597/

相关文章:

c# - 表单时未处理计时器

c# - 字母数字文本框 - 粘贴前验证/清理剪贴板文本

java - 如何创建 GUI 网格并将每个结果放入每个网格中?

java - 将 ScrollableComposite 位置设置为位置

user-interface - JavaFX 中的自定义节点绘制

c# - 在 C# 中运行 Windows 窗体时访问和更改 txt 文档

c# - 在非常复杂的多任务窗体程序中锁定和使用 Entity Framework 上下文是否可以?

powershell - PowerShell 短语 "| ? {...}"是什么意思?

使用命令提示符在两个文本之间获取文本的正则表达式

powershell - 从模块内部访问 PowerShell 脚本参数