In my last blog post I showed how to work with the ListView in PowerShell Studio to add Groups and Items.
Now I will show how easy it is to add Groups with PowerShell code so this will be more flexible.
Add a ListView Control to a new form.
Change the following properties:
CheckBoxes
True
View
Details
Alignment
Left
Columns
Add a default column
And then it’s thime for the code:
$myGroups = "Group One","Group Two","Group Three" $listview1.BeginUpdate() foreach ($myGroup in $myGroups) { $listview1.Groups.Add($myGroup) } $listview1.EndUpdate()
BeginUpdate and EndUpdate is best to use to have the ListView Control in a good state.
After this our form will look like this:
We need to add Items to be able to see our Groups.
$myItems = "One","Two","Three" foreach ($myItem in $myItems) { Add-ListViewItem -ListView $listview1 -Items $myItem }
This is very easy when working with PowerShell Studio.
When adding a ListView Control a function for adding Items is added as well.
When adding ListView items to a specific group we need to specifiy that when adding the Item.
Now we have added all items to the default Group.
To add a specific item to a specific Group we need to do like this:
Add-ListViewItem -ListView $listview1 -Items "One" -Group $listview1.Groups[0] Add-ListViewItem -ListView $listview1 -Items "Two" -Group $listview1.Groups[1] Add-ListViewItem -ListView $listview1 -Items "Three" -Group $listview1.Groups[2]
Yes, we can’t address the Groups with names
You need address it as an Array.
But there are several ways to go round that.
I will show you how I have done in my next post.