创建自定义DataGridView

创建自定义DataGridView

本文介绍了Powershell:创建自定义DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图创建一个自定义Datagridview放入我制作的GUI程序中,但是遇到了一些麻烦。



到目前为止

  $ form =新对象System.Windows.Forms.Form 
$ form.Size =新- Object System.Drawing.Size(900,600)
$ dataGridView =新对象System.Windows.Forms.DataGridView
$ dataGridView.Size =新对象System.Drawing.Size(800,400)

$ form.Controls.Add($ dataGridView)
$ dataGridView.ColumnCount = 4
$ dataGridView.ColumnHeadersVisible = $ true
$ dataGridView.Columns [0] .Name =处理
$ dataGridView.Columns [1] .Name = ID
$ dataGridView.Columns [2] .Name =描述
$ dataGridView.Columns [3] .Name = 内存

$ row1 =获取进程的属性名称|选择名称

$ rows = @($ row1)
foreach($ row in $ rows)
{
$ dataGridView.Rows.Add($ row.name )}
$ form.ShowDialog()

我的问题是:



我该如何为不同的属性分配不同的列,因此 process列用于进程名称, id列用于进程ID,依此类推。 / p>

到目前为止,我所要做的就是为一列分配一个输入范围:进程名称。



请帮助!



谢谢

解决方案

在所有进程中都处于循环状态,并按照定义列的顺序添加每个流程属性:

  get-process | foreach {
$ dataGridView.Rows.Add($ _。Name,$ _。ID,$ _。Description,$ _。WorkingSet)
}

您还可以通过选择要显示的属性来动态生成列,每个属性都将成为列名,并使用网格的DataSource属性和Array列表将对象添加到网格中:

  $ form = New-Object System.Windows.Forms.Form 
$ form.Size =新对象System.Drawing.Size(900,600)

$ gps =获取过程|选择名称,ID,说明,@ {n ='内存'; e = {$ _。WorkingSet}}
$ list =新对象System.collections.ArrayList
$ list.AddRange($ gps )

$ dataGridView =新对象System.Windows.Forms.DataGridView-属性@ {
Size = New-Object System.Drawing.Size(800,400)
ColumnHeadersVisible = $ true
DataSource = $ list
}

$ form.Controls.Add($ dataGridView)
$ form.ShowDialog()


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()

这篇关于Powershell:创建自定义DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:32