PowerShell的:创建自定义Datagriview [英] Powershell: Creating Custom Datagriview

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

问题描述

所以我想创建一个自定义的DataGridView投入我做了一个GUI程序,但我有一些麻烦。

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:

我如何去分配不同的列不同的充性能,所以列'过程'将是procress名,列'ID'将是进程ID等。

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.

请帮帮忙!

感谢

推荐答案

环路上的所有进程,并在已定义的列的顺序添加的每个进程属性

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

您也可以生成栏目动态地选择要显示的属性,每个属性变成了列名,并使用网格的DataSource属性和数组列表的对象添加到网格中:

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的:创建自定义Datagriview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆