2015-02-08 155 views
0

我有以下腳本,它可以搜索所有驅動器中的PST文件並將它們顯示在datagridview中。 當我想將PST文件複製到新位置時,我一次只能選擇1個文件。有沒有一種方法可以選擇多行(PST文件)並將其複製到1中去?在datagridview中選擇多行

#Generated Form Function 
function GenerateForm { 
######################################################################## 
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 
# Generated On: 05/02/2015 13:48 
# Generated By: Andy 
######################################################################## 

#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 
$statusBar = New-Object System.Windows.Forms.StatusBar 
$btn_copy = New-Object System.Windows.Forms.Button 
$btn_close = New-Object System.Windows.Forms.Button 
$btn_Search = New-Object System.Windows.Forms.Button 
$dataGrid1 = New-Object System.Windows.Forms.DataGridview 
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState 
#endregion Generated Form Objects 

#---------------------------------------------- 
#Generated Event Script Blocks 
#---------------------------------------------- 

function Get-PSTInfo { 
     $statusbar.text = "Searching " 
     $array = New-Object System.Collections.ArrayList 
     $Script:pstinfo = Get-PSDrive -PSProvider "filesystem"|%{get-childitem $_.root -include *.pst -r}|select name, directoryname, @{name="Size (GB)";expression ={"{0:N2}" -f ($_.length/1GB)}} 
     $array.AddRange($pstinfo) 
     $dataGrid1.DataSource = $array 
     $dataGrid1.autosize = $false 
     $form1.refresh() 
     $statusbar.text = "Finished Searching" 
     $array|Export-Csv c:\temp\pstfound.csv -notypeinformation 
     } 

$btn_close_OnClick= 
{ 
$form1.close() 
} 

$btn_copy_OnClick= 
{ 
    $Outlook = New-Object -Com Outlook.Application 
    $Outlook.Quit() 
    sleep 5 
    $selection = $datagrid1.selectedrows[0].databounditem 
    $file=$selection.fullname 
    $file|Out-File -Encoding ascii -filepath "C:\temp\pstcopied.txt" -append 
    copy $file c:\temp 
} 

$btn_Search_OnClick= 
{ 
    $strFileName = "c:\temp\pstfound.csv" 
     If (Test-Path $strFileName){ 
      Remove-Item $strFileName 
      } 
    get-pstinfo 
} 

$OnLoadForm_StateCorrection= 
{#Correct the initial state of the form to prevent the .Net maximized form issue 
    $form1.WindowState = $InitialFormWindowState 
} 

#---------------------------------------------- 
#region Generated Form Code 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 417 
$System_Drawing_Size.Width = 610 
$form1.ClientSize = $System_Drawing_Size 
$form1.DataBindings.DefaultDataSourceUpdateMode = 0 
$form1.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('D:\Documents\SAPIEN\PowerShell Studio 2012\Files\atsign.ico') 
$form1.Name = "form1" 
$form1.Text = "PST Search" 

$statusBar.DataBindings.DefaultDataSourceUpdateMode = 0 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 0 
$System_Drawing_Point.Y = 395 
$statusBar.Location = $System_Drawing_Point 
$statusBar.Name = "statusBar" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 22 
$System_Drawing_Size.Width = 610 
$statusBar.Size = $System_Drawing_Size 
$statusBar.TabIndex = 4 

$form1.Controls.Add($statusBar) 


$btn_copy.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 126 
$System_Drawing_Point.Y = 357 
$btn_copy.Location = $System_Drawing_Point 
$btn_copy.Name = "btn_copy" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 28 
$System_Drawing_Size.Width = 82 
$btn_copy.Size = $System_Drawing_Size 
$btn_copy.TabIndex = 3 
$btn_copy.Text = "Copy" 
$btn_copy.UseVisualStyleBackColor = $True 
$btn_copy.add_Click($btn_copy_OnClick) 

$form1.Controls.Add($btn_copy) 


$btn_close.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 242 
$System_Drawing_Point.Y = 357 
$btn_close.Location = $System_Drawing_Point 
$btn_close.Name = "btn_close" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 28 
$System_Drawing_Size.Width = 82 
$btn_close.Size = $System_Drawing_Size 
$btn_close.TabIndex = 2 
$btn_close.Text = "Close" 
$btn_close.UseVisualStyleBackColor = $True 
$btn_close.add_Click($btn_close_OnClick) 

$form1.Controls.Add($btn_close) 


$btn_Search.DataBindings.DefaultDataSourceUpdateMode = 0 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 13 
$System_Drawing_Point.Y = 357 
$btn_Search.Location = $System_Drawing_Point 
$btn_Search.Name = "btn_Search" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 28 
$System_Drawing_Size.Width = 82 
$btn_Search.Size = $System_Drawing_Size 
$btn_Search.TabIndex = 1 
$btn_Search.Text = "Search" 
$btn_Search.UseVisualStyleBackColor = $True 
$btn_Search.add_Click($btn_Search_OnClick) 

$form1.Controls.Add($btn_Search) 

$dataGrid1.DataBindings.DefaultDataSourceUpdateMode = 0 
$dataGrid1.DataMember = "" 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 13 
$System_Drawing_Point.Y = 13 
$dataGrid1.Location = $System_Drawing_Point 
$dataGrid1.Name = "dataGrid1" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Height = 335 
$System_Drawing_Size.Width = 579 
$dataGrid1.Size = $System_Drawing_Size 
$dataGrid1.TabIndex = 0 

$form1.Controls.Add($dataGrid1) 

#endregion Generated Form Code 

#Save the initial state of the form 
$InitialFormWindowState = $form1.WindowState 
#Init the OnLoad event to correct the initial state of the form 
$form1.add_Load($OnLoadForm_StateCorrection) 
#Show the Form 
$form1.ShowDialog()| Out-Null 

} #End Function 

#Call the Function 
GenerateForm 

回答

1

我無意中在您的other question中解決了這個問題。您需要設置的DataGridView

$dataGridView.SelectionMode = 'FullRowSelect' 
$dataGridView.MultiSelect = $true 

這些屬性和在我的例子,這是我如何檢索到的數據作爲一個PowerShell對象。代碼被添加到按鈕on_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 
}