- $ComputersList = Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' | Select -ExpandProperty Name
Create a DataGridView and export to a csv
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.
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.
Re: Create a DataGridView and export to a csv
But if i want to add $ComputerList, how can i do ? I can't believe that there isn' t a way
Re: Create a DataGridView and export to a csv
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.
$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.
- $ComputersList = Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' | Select -ExpandProperty Names
- $i0 = $dataGridView.Columns.Add('COMPUTERNAME', 'COMPUTERNAME')
- $c0 = $dataGridView.Columns[$i0]
- $c0.width = 150
- $c0.DataPropertyName = 'COMPUTERNAME'
- #[void]$c0.Items.AddRange($ComputersList)
- [void]$dt.Columns.Add($c0.DataPropertyName)
- Attachments
-
- 2.jpg (31.75 KiB) Viewed 2636 times
Re: Create a DataGridView and export to a csv
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,''))
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,''))
Re: Create a DataGridView and export to a csv
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
I have to put the computer list in the first column called "COMPUTERNAME", the object is a datatable
- #Create a datable and assign it to DataGridView
- $dt = New-Object System.Data.DataTable
- $i0 = $dataGridView.Columns.Add('COMPUTERNAME', 'COMPUTERNAME')
- $c0 = $dataGridView.Columns[$i0]
- $c0.width = 150
- $c0.DataPropertyName = 'COMPUTERNAME'
- [void]$c0.Items.AddRange($ComputersList)
- [void]$dt.Columns.Add($c0.DataPropertyName)
- Attachments
-
- 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.
Re: Create a DataGridView and export to a csv
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.
Re: Create a DataGridView and export to a csv
The second column and dataTable are already created this is the full code
- #Create a datable and assign it to DataGridView
- $dt = New-Object System.Data.DataTable
- $i0 = $dataGridView.Columns.Add('COMPUTERNAME', 'COMPUTERNAME')
- $c0 = $dataGridView.Columns[$i0]
- $c0.width = 150
- $c0.DataPropertyName = 'COMPUTERNAME'
- [void]$c0.Items.AddRange($ComputersList)
- [void]$dt.Columns.Add($c0.DataPropertyName)
- $UserList = $Global:SelectedOU | ForEach { Get-ADUser -Filter * -SearchBase $_ <#'OU=DOMUSERS,DC=DOM,DC=LOCAL'#> | Select -ExpandProperty SamAccountName}
- $c1 = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
- [void]$datagridview.Columns.Add($c1)
- $c1 = $datagridview.Columns[1]
- $c1.FlatStyle = "Flat"
- $c1.DataPropertyName = "DOMAIN ACCOUNT"
- $c1.HeaderText = "DOMAIN ACCOUNT"
- $c1.Width = 150
- $c1.HeaderCell.Style.Alignment = "middlecenter"
- $c1.DefaultCellStyle.Alignment = "middlecenter"
- [void]$c1.Items.AddRange($UserList)
- [void]$dt.Columns.Add($c1.DataPropertyName)
Re: Create a DataGridView and export to a csv
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.
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.
Re: Create a DataGridView and export to a csv
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.
$ 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.
Re: Create a DataGridView and export to a csv
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.
Be sure your userlist contains one empty string as you are adding the users column as an empty string.