Custom column name in datagridview

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
paddy75
Posts: 27
Last visit: Sat Mar 16, 2024 11:11 pm

Custom column name in datagridview

Post by paddy75 »

Product: PowerShell Studio 2021 (64 Bit)
Build: v5.8.195
OS: Windows 10 Pro (64 Bit)
Build: v10.0.19043.0


Hello all,
I have a listview with checkboxes that is filled by an xml-file. Listview has items and subitems. Depends on what is checked, the attributes from active directory will be load to a datagridview and also when I change the items, the datagridview gets the updated items.
1.png
1.png (5.21 KiB) Viewed 2292 times
2.png
2.png (3.45 KiB) Viewed 2292 times
This is working fine for now. What I try to do is, to change the column headers to the subitems from the listview (sn should be Lastname, givenName should be Firstname and so on)

#
  1. get all checked attributes from listview
  2. $checkedAttributes=@()
  3. ForEach ($item In $lstviewAttribute.CheckedItems)
  4. {
  5.     $checkedAttributes = $checkedAttributes + $item.Text
  6. }
  7.        
  8. #get all enabled user for the choosen attributes
  9. $ADUsers = Get-ADUser -LDAPFilter '(!userAccountControl:1.2.840.113556.1.4.803:=2)' -Properties * -SearchBase $($lblSearchbaseAttributes.Text) | Select-Object $checkedAttributes
  10.        
  11. #convert the results
  12. $ADUsersFinal = ConvertTo-DataTable -InputObject $ADUsers
  13.    
  14. #fill the datagridview with the final results
  15. Update-DataGridView -DataGridView $dtgAttributes -Item $ADUsersFinal

But I have no idea how I can achieve this. Hope someone has some tips for me.
PS: sorry for my bad english.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Custom column name in datagridview

Post by jvierra »

You are specifying that the columns are the properties of the object retrieved. Those will be the column headers. Don't select properties you don't want. You can also use a computed "Select-Object" to reassign names to any property.

My best guess is that you either inherited the code or copied it from the Internet. Look at how the ListView is built and you will see how the "DisplayName" of the properties was acquired.

Use the computed select to create aa column with the display name instead of the property name.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Custom column name in datagridview

Post by jvierra »

Here is the best way to load a DGV. THe way you are doing it does the same conversion twice. Totally unnecessary.
  1. #get all checked attributes from listview
  2. $checkedAttributes=@()
  3. ForEach ($item In $lstviewAttribute.CheckedItems){
  4.     $checkedAttributes = $checkedAttributes += $item.Text
  5. }
  6.        
  7. #get all enabled user for the choosen attributes
  8. $ADUsers = Get-ADUser -LDAPFilter '(!userAccountControl:1.2.840.113556.1.4.803:=2)' -Properties * -SearchBase $lblSearchbaseAttributes.Text|
  9.     Select-Object $checkedAttributes
  10.        
  11. #fill the datagridview with the final results
  12. $dtgAttributes.DataSource = ConvertTo-DataTable -InputObject $ADUsers
Change the $items.Text to the subitem text for the display name or swap the item for the subitem in the ListView.
paddy75
Posts: 27
Last visit: Sat Mar 16, 2024 11:11 pm

Re: Custom column name in datagridview

Post by paddy75 »

Thanks for the reply.
I modified my code to load a datagridview, thanks for this note.
I don't understand or we're not on the same page. :?:

First I check for all choosen attributes from the listview.

Code: Select all

#get all checked attributes from listview
$checkedAttributes=@()
ForEach ($item In $lstviewAttribute.CheckedItems){
    $checkedAttributes = $checkedAttributes += $item.Text
}
Then i use $checkedAttributes to search all enabled accounts to get back the choosen attributes from Active Directory.

Code: Select all

$ADUsers = Get-ADUser -LDAPFilter '(!userAccountControl:1.2.840.113556.1.4.803:=2)' -Properties * -SearchBase $lblSearchbaseAttributes.Text|
    Select-Object $checkedAttributes
I only want to have the item.subitems.text in the datagridview because this is more userfriendly to see for what this attribute is used for (extensionAttribute1 is use for laboratory users for example).

When you talk about "computed select" you mean "calculated properties"?



When I swap item.text with item.subitems then the Get-ADUser will not work anymore.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Custom column name in datagridview

Post by jvierra »

First you need to learn how to use things like the debugger. Next take some time to learn about how the controls work.

Yes. "Calculated properties" which are also referred to as "computed" properties since the words "computed" and "calculated" are pretty much identical in meaning.
This topic is 2 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked