简体   繁体   中英

Can't populate DataGridView column with calendar meeting property

I'm working on a little side project of listing outlook calendar meetings in the form data grid view. I can populate columns with Subject and Start Date with no problem. For some reason, I can not populate a column with .DayOfWeekMask value, even though I can access this property via CLI. Any thoughts? Code below

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

update you datagrid wiht this one

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

======= this datasourge work eveytime..

Based on the answer of @Kemal I removed .ColumnCount and subsequent Name properties. Created new ArrayList via $array = [System.Collections.ArrayList]::new() abd then applied Kemal's solution. In th end the script looks like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM