填充datagridview [英] Populating a datagridview
问题描述
我可以使用以下命令找到它: -
Get-PSDrive -PSProviderfilesystem|%{get-childitem $ _。root -include * .pst -r} | select name,directoryname ,@ {name =Size(GB); expression = {{0:N2}-f($ _。length / 1GB)}}
唯一的问题是需要大约45分钟的时间才能运行所有驱动器并完成搜索。我正在想通过使用Windows搜索索引来加快速度。
我有这个....
function searchindex {
$ query =SELECT System.ItemName,system.ItemPathDisplay,System.ItemTypeText,System.Size FROM SystemIndex其中system.itemtypetext ='outlook数据文件'
$ objConnection = New-Object -ComObject adodb.connection
$ objrecordset = New-Object -ComObject adodb.recordset
$ objconnection.open(Provider = Search.CollatorDSO; Extended属性='Application = Windows';)
$ objrecordset.open($ query,$ objConnection)
$ array = @()
尝试{$ objrecordset.MoveFirst()}
Catch [system.exception] {no records returned}
do
{
写主机($ objrecordset.Fields.Item(System 。$`
($ objrecordset.Fields.Item(System.ItemPathDisplay))。$`
($ objrecordset.Fields.Item(System.ITemTypeText))。值
($ objrecordset.Fields.Item(System.Size))value
if(-not($ objrecordset.EOF)){$ objrecordset.MoveNext()}
} Until($ objrecordset.EOF)
$ objrecordset.Close()
$ objConnection.Close()
$ objrecordset = $ null
$ objConnection = $ null
[gc] :: collect()
}
这将在几秒内将详细信息输出到屏幕,这是完美的,但我无法解决如何在datagrid视图。
我正在使用原始形式创建表单。
一旦数据被填充到datagridview我想要能够选择记录并将它们复制到新的位置
任何人都可以帮助?
TIA
Andy
我不熟悉 DataGridView
但我觉得如果你有一个对象,你将更有能力操纵它。
function Searchindex {
$ query = SELECT System.ItemName,system.ItemPathDisplay,System.ItemTypeText,System.Size FROM SystemIndex其中system.itemtypetext ='outlook数据文件'
$ objConnection = New-Object -ComObject adodb.connection
$ objrecordset = New-Object -ComObject adodb.recordset
$ objconnection.open(Provider = Search.CollatorDSO; Extended Properties ='Application = Windows';)
$ objrecordset.open($ query,$ objConnection )
$ array = @()
尝试{$ objrecordset.MoveFirst()}
Catch [system.exception] {no records returned}
do
{
$ array + = [pscustomobject] @ {
Name =($ objrecordset.Fields.Item(System.ItemName))value
路径=($ objrecordset.Fields.Item(System.ItemPathDisplay))value
TypeText =($ objrecordset.Fields.Item(System.ITemTypeText))value
Size =($ objrecordset .Fields.Item (System.Size))value
}
如果(-not($ objrecordset.EOF)){$ objrecordset.MoveNext()}
}直到($ objrecordset.EOF)
$ objrecordset.Close()
$ objConnection.Close()
$ objrecordset = $ null
$ objConnection = $ null
[gc] :: collect()
$ array
}
这将发送一个自定义的PowerShell对象数组。您已经将变量 $ array
初始化。我们只需要填充它。
然后你可以使用这样的东西过滤出你要找的文件。
Searchindex | Out-GridView -PassThru
击中 Ok
它只会输出所选记录。
DataGridView
具有多重选择和返回
$ global:results = @()
#... searchindex函数在这里...
$ form =新对象System.Windows.Forms.Form
$ form.Size =新对象System.Drawing.Size(900,600)
$ dataGridView = New -Object System.Windows.Forms.DataGridView
$ dataGridView.Size = New-Object System.Drawing.Size(800,400)
$ dataGridView.SelectionMode ='FullRowSelect'
$ dataGridView.MultiSelect = $ true
$ go = New-Object System.Windows.Forms.Button
$ go.Location = New-Object System.Drawing.Size(300,450)
$ go.Size = New-对象System.Drawing.Size(75,23)
$ go.text =选择
$ form.Controls.Add($ go)
$ form.Controls.Add($ dataGridView )
$ arraylist =新对象System.Collections.ArrayList
$ arraylist.AddRange((Searchindex))
$ dataGridView.DataSource = $ arraylist
$ dat aGridView.Columns [0] .width = 240
$ go.Add_Click(
{
$ dataGridView.SelectedRows | ForEach-Object {
$ global:results + = [pscustomobject] @ {
Name = $ dataGridView.Rows [$ _。Index] .Cells [0] .Value
Path = $ dataGridView .Rows [$ _。索引] .Cells [1] .Value
TypeText = $ dataGridView.Rows [$ _。Index] .Cells [2] .Value
Size = $ dataGridView.Rows [$ _.Index] .Cells [3] .Value
}
$ form.Close()
}
})
$ form.ShowDialog()
$ global:results
这里有很多东西要覆盖看看这些例子,让我知道这对你有用。它将返回所有选定的行作为全局变量 $ global:results
中的对象。它需要是全局的,因为输出不会保留在 $ go.Add_Click
之外。 searchindex
函数在第二个代码示例中省略,以节省空间。
I ma working on a form that will search all connected drives for PST files.
I can get it working with the following command:-
Get-PSDrive -PSProvider "filesystem"|%{get-childitem $_.root -include *.pst -r}|select name, directoryname, @{name="Size (GB)";expression ={"{0:N2}" -f ($_.length/1GB)}}
The only problem is it takes about 45 minutes to run through all the drives and finish the search. I was thinking of trying to speed it up by using the windows search index.
I have got this....
function Searchindex{
$query="SELECT System.ItemName, system.ItemPathDisplay, System.ItemTypeText, System.Size FROM SystemIndex where system.itemtypetext = 'outlook data file'"
$objConnection = New-Object -ComObject adodb.connection
$objrecordset = New-Object -ComObject adodb.recordset
$objconnection.open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")
$objrecordset.open($query, $objConnection)
$array=@()
Try { $objrecordset.MoveFirst() }
Catch [system.exception] { "no records returned" }
do
{
Write-host ($objrecordset.Fields.Item("System.ItemName")).value `
($objrecordset.Fields.Item("System.ItemPathDisplay")).value `
($objrecordset.Fields.Item("System.ITemTypeText")).value `
($objrecordset.Fields.Item("System.Size")).value
if(-not($objrecordset.EOF)) {$objrecordset.MoveNext()}
} Until ($objrecordset.EOF)
$objrecordset.Close()
$objConnection.Close()
$objrecordset = $null
$objConnection = $null
[gc]::collect()
}
this outputs the details to the screen in a few seconds which is perfect but I can't work out how to display it in a datagrid view.
I am using primal form to create the forms.
Once the data is populated in the datagridview I want to be able to select records and copy them to a new location
Can anyone help?
TIA
Andy
I'm not familiar with DataGridView
but I feel if you had an object you would be better capable to manipulate it.
function Searchindex{
$query="SELECT System.ItemName, system.ItemPathDisplay, System.ItemTypeText, System.Size FROM SystemIndex where system.itemtypetext = 'outlook data file'"
$objConnection = New-Object -ComObject adodb.connection
$objrecordset = New-Object -ComObject adodb.recordset
$objconnection.open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")
$objrecordset.open($query, $objConnection)
$array=@()
Try { $objrecordset.MoveFirst() }
Catch [system.exception] { "no records returned" }
do
{
$array += [pscustomobject]@{
Name = ($objrecordset.Fields.Item("System.ItemName")).value
Path = ($objrecordset.Fields.Item("System.ItemPathDisplay")).value
TypeText = ($objrecordset.Fields.Item("System.ITemTypeText")).value
Size = ($objrecordset.Fields.Item("System.Size")).value
}
If(-not($objrecordset.EOF)) {$objrecordset.MoveNext()}
} Until ($objrecordset.EOF)
$objrecordset.Close()
$objConnection.Close()
$objrecordset = $null
$objConnection = $null
[gc]::collect()
$array
}
This will send out a custom PowerShell object array. You already had the variable $array
initialized. We just needed to populate it.
Then you could use something like this to filter out the files you are looking for.
Searchindex | Out-GridView -PassThru
After hitting Ok
it will only output the records selected.
DataGridView
with multiselect and return
$global:results = @()
#...searchindex function is here ....
$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)
$dataGridView.SelectionMode = 'FullRowSelect'
$dataGridView.MultiSelect = $true
$go = New-Object System.Windows.Forms.Button
$go.Location = New-Object System.Drawing.Size(300,450)
$go.Size = New-Object System.Drawing.Size(75,23)
$go.text = "Select"
$form.Controls.Add($go)
$form.Controls.Add($dataGridView)
$arraylist = New-Object System.Collections.ArrayList
$arraylist.AddRange((Searchindex))
$dataGridView.DataSource = $arraylist
$dataGridView.Columns[0].width = 240
$go.Add_Click(
{
$dataGridView.SelectedRows| ForEach-Object{
$global:results += [pscustomobject]@{
Name = $dataGridView.Rows[$_.Index].Cells[0].Value
Path = $dataGridView.Rows[$_.Index].Cells[1].Value
TypeText = $dataGridView.Rows[$_.Index].Cells[2].Value
Size = $dataGridView.Rows[$_.Index].Cells[3].Value
}
$form.Close()
}
})
$form.ShowDialog()
$global:results
There is a lot to cover here but look at the examples and let me know how this works for you. It will return all selected rows back as objects in the global variable $global:results
. It needs to be global as output does not persist outside the $go.Add_Click
. The searchindex
function is there but omitted in the second code sample to save space.
这篇关于填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!