Create a DataGridView and export to a csv

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 6 years and 10 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
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Create a DataGridView and export to a csv

Post by ramses147 »

But if i want to add $ComputerList, how can i do ? I can't believe that there isn' t a way

  1. $ComputersList = Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' | Select -ExpandProperty Name
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Create a DataGridView and export to a csv

Post by jvierra »

What control are you adding it to?

YOu have to be clear and understand how the controls work. A cell or column cannot be added as a list.
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Create a DataGridView and export to a csv

Post by ramses147 »

Sorry trying explaing easier:

$ComputersList extract from domain controller the pc names, i want to add in the first column the pc names like in the picture ( Obviously in the photo I entered the names manually )
The second column works, I have no problems, it is a combobox from which domain users are chosen

So the goal is to automatically insert the pc into the COMPUTERNAME column.
  1. $ComputersList = Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' | Select -ExpandProperty Names
  2. $i0 = $dataGridView.Columns.Add('COMPUTERNAME', 'COMPUTERNAME')
  3. $c0 = $dataGridView.Columns[$i0]
  4. $c0.width = 150
  5. $c0.DataPropertyName = 'COMPUTERNAME'
  6. #[void]$c0.Items.AddRange($ComputersList)
  7. [void]$dt.Columns.Add($c0.DataPropertyName)
Attachments
2.jpg
2.jpg (31.75 KiB) Viewed 2636 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Create a DataGridView and export to a csv

Post by jvierra »

You will have to add a complete row or create a data table or collection that specifies complete rows. The DataNameProperty must match the table or collection name.

Items = Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' | Select Name, @{n='DomainAccount';e={''}}
$DataGridView.DataSource = [System.Collections.ArrayList]$items


If you add rows then you must add all columns to each added row.

$datagridview.Rows.Add(@($name,''))
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Create a DataGridView and export to a csv

Post by ramses147 »

I want to add only values in the first column, like in the picture
I have to put the computer list in the first column called "COMPUTERNAME", the object is a datatable
  1. #Create a datable and assign it to DataGridView
  2. $dt = New-Object System.Data.DataTable
  3.  
  4. $i0 = $dataGridView.Columns.Add('COMPUTERNAME', 'COMPUTERNAME')
  5. $c0 = $dataGridView.Columns[$i0]
  6. $c0.width = 150
  7. $c0.DataPropertyName = 'COMPUTERNAME'
  8. [void]$c0.Items.AddRange($ComputersList)
  9. [void]$dt.Columns.Add($c0.DataPropertyName)
Attachments
2.jpg
2.jpg (21.18 KiB) Viewed 2629 times
Last edited by ramses147 on Wed May 24, 2017 8:08 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Create a DataGridView and export to a csv

Post by jvierra »

You need to create a DataTable from the generated list as I showed you before. If the table needs two columns and you only have one then generate as I posted in my previous post.
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Create a DataGridView and export to a csv

Post by ramses147 »

The second column and dataTable are already created this is the full code
  1. #Create a datable and assign it to DataGridView
  2. $dt = New-Object System.Data.DataTable
  3.  
  4. $i0 = $dataGridView.Columns.Add('COMPUTERNAME', 'COMPUTERNAME')
  5. $c0 = $dataGridView.Columns[$i0]
  6. $c0.width = 150
  7. $c0.DataPropertyName = 'COMPUTERNAME'
  8. [void]$c0.Items.AddRange($ComputersList)
  9. [void]$dt.Columns.Add($c0.DataPropertyName)
  10.  
  11. $UserList = $Global:SelectedOU | ForEach { Get-ADUser -Filter * -SearchBase $_ <#'OU=DOMUSERS,DC=DOM,DC=LOCAL'#> | Select -ExpandProperty SamAccountName}
  12. $c1 = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
  13. [void]$datagridview.Columns.Add($c1)
  14. $c1 = $datagridview.Columns[1]
  15. $c1.FlatStyle = "Flat"
  16. $c1.DataPropertyName = "DOMAIN ACCOUNT"
  17. $c1.HeaderText = "DOMAIN ACCOUNT"
  18. $c1.Width = 150
  19. $c1.HeaderCell.Style.Alignment = "middlecenter"
  20. $c1.DefaultCellStyle.Alignment = "middlecenter"
  21. [void]$c1.Items.AddRange($UserList)
  22. [void]$dt.Columns.Add($c1.DataPropertyName)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Create a DataGridView and export to a csv

Post by jvierra »

You cannot add items to a column.

You must create a data table and load it with data.

$dt.Rows.Add(@('coll1','col2'))

First create the datatable and load it. Next create the grid. Or --- load the datatable after creating both.
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Create a DataGridView and export to a csv

Post by ramses147 »

I can not understand, what you have written can not work in my case.
$ ComputerList has its command to extract computer names.
$ UserList has another command to extract users as you see.
The "computername" column will be populated by the values contained in $ computerList, while the second column is a combobox and is already ready with the code I wrote above.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Create a DataGridView and export to a csv

Post by jvierra »

If you follow my instructions you will see how it works.

Be sure your userlist contains one empty string as you are adding the users column as an empty string.
This topic is 6 years and 10 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