簡體   English   中英

無法使用日歷會議屬性填充 DataGridView 列

[英]Can't populate DataGridView column with calendar meeting property

我正在做一個小項目,在表格數據網格視圖中列出 outlook 日歷會議。 我可以毫無問題地用主題和開始日期填充列。 出於某種原因,我無法使用.DayOfWeekMask值填充列,即使我可以通過 CLI 訪問此屬性。 有什么想法嗎? 下面的代碼

add-type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'

$4 = 
"BASE64 code"

$today = get-date 

$Script:selection = @()

add-type -AssemblyName "Microsoft.Office.Interop.Outlook"
$outlookFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNamespace("MAPI")
$folder = $namespace.GetDefaultFolder($outlookFolders::olFolderCalendar)
$items = $folder.Items
$meetings = $items|?{$_.isRecurring -eq $true -and $_.Subject -notlike "*Canceled*" <#-and $_.GetRecurrencePattern().DayOfWeekMask -eq 18#>}|Select Subject, Start, @{l='Mask';e={$_.GetRecurrencePattern().DayOfWeekMask}}

$bitmap3 = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap3.BeginInit()
$bitmap3.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($4)
$bitmap3.EndInit()
$bitmap3.Freeze()

$image3 = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream($bitmap3.StreamSource)

$icon3 = [System.Drawing.Icon]::FromHandle($image3.GetHicon())

$main2 = New-Object System.Windows.Forms.Form
$main2.Icon = $icon3
$main2.ClientSize = '600,600'
$main2.MinimizeBox = $false
$main2.MaximizeBox = $false
$main2.Text = 'Options'  
$main2.AutoSize = $true 

$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = '5,5'
$grid.Height = 500
$grid.Width  = 390
$grid.ColumnHeadersVisible = $true
$grid.AutoSizeColumnsMode = 'AllCells'
$grid.ColumnCount = 3
$grid.columns[0].Name = 'Name'
$grid.Columns[1].Name = 'Time'
$grid.Columns[2].Name = 'Day Mask'
$grid.MultiSelect = $true
$grid.SelectionMode = 'FullRowSelect'

$meetings|%{$grid.Rows.Add($($_.Subject), $($_.Start)), $($_.Mask)}

$button = [System.Windows.Forms.Button]::new()
$button.Location = [System.Drawing.Point]::new(5,540)
$button.Size = [System.Drawing.Size]::new(100,50)
$button.Text = 'OK'
$button.Add_Click({

$grid.SelectedRows|%{
$Script:selection += [pscustomobject]@{
                           Name = $grid.Rows[$_.Index].Cells[0].Value
                           Time = $grid.Rows[$_.Index].Cells[1].Value
                           }
                    }

})

$main2.Controls.AddRange(@($grid,$button))
$main2.ShowDialog()

用這個更新你的數據網格

$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = '5,5'
$grid.Height = 500
$grid.Width  = 390
$grid.ColumnHeadersVisible = $true
$grid.AutoSizeColumnsMode = 'AllCells'
$grid.MultiSelect = $true
$grid.SelectionMode = 'FullRowSelect'
$array.AddRange($meetings)
$grid.DataSource=($array)

======= 這個 datasourge 工作 everytime..

根據@Kemal 的回答,我刪除.ColumnCount和后續的Name屬性。 通過$array = [System.Collections.ArrayList]::new() abd 創建新的 ArrayList 然后應用 Kemal 的解決方案。 最后腳本看起來像這樣。

add-type -AssemblyName 'System.Windows.Forms'
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'PresentationFramework'

$4 = 
"BASE64 CODE"

$today = get-date 
$selection =@()
$array = [System.Collections.ArrayList]::new()


add-type -AssemblyName "Microsoft.Office.Interop.Outlook"
$outlookFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace = $outlook.GetNamespace("MAPI")
$folder = $namespace.GetDefaultFolder($outlookFolders::olFolderCalendar)
$items = $folder.Items
$meetings = $items|?{$_.isRecurring -eq $true -and $_.Subject -notlike "*Canceled*" |Select Subject, Start, @{l='Mask';e={$_.GetRecurrencePattern().DayOfWeekMask}}

$bitmap3 = New-Object System.Windows.Media.Imaging.BitmapImage
$bitmap3.BeginInit()
$bitmap3.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($4)
$bitmap3.EndInit()
$bitmap3.Freeze()

$image3 = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream($bitmap3.StreamSource)

$icon3 = [System.Drawing.Icon]::FromHandle($image3.GetHicon())


$main2 = New-Object System.Windows.Forms.Form
$main2.Icon = $icon3
$main2.ClientSize = '600,600'
$main2.MinimizeBox = $false
$main2.MaximizeBox = $false
$main2.Text = 'Options'  
$main2.AutoSize = $true 




$grid = New-Object System.Windows.Forms.DataGridView
$grid.Location = '5,5'
$grid.Height = 500
$grid.Width  = 390
$grid.ColumnHeadersVisible = $true
$grid.AutoSizeColumnsMode = 'AllCells'
$grid.MultiSelect = $true
$grid.SelectionMode = 'FullRowSelect'
$array.AddRange($meetings)
$grid.DataSource=($array)


$button = [System.Windows.Forms.Button]::new()
$button.Location = [System.Drawing.Point]::new(5,540)
$button.Size = [System.Drawing.Size]::new(100,50)
$button.Text = 'OK'
$button.Add_Click({

$grid.SelectedRows|%{
$Script:selection += [pscustomobject]@{
                           Name = $grid.Rows[$_.Index].Cells[0].Value
                           Time = $grid.Rows[$_.Index].Cells[1].Value
                           DayofWeek = $grid.Rows[$_.Index].Cells[2].Value

}
}
})

$main2.Controls.AddRange(@($grid,$button))
$main2.ShowDialog()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM