Add ComboBoxColum

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 7 years and 9 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
DanyGr
Posts: 5
Last visit: Tue Apr 06, 2021 8:07 am

Add ComboBoxColum

Post by DanyGr »

Product, version and build: PowerShell Studio 2016 Version 5.2.119
32 or 64 bit version of product: 64 bit
Operating system: Windows 7
32 or 64 bit OS: 64 bit
PowerShell Version: 4.0


Hi,
I grab this example file from a previous post, «Demo-DGVDataTable.psf» and I would like to know how can I add ComboBoxColum for the column «Age» for example or add a new column?

Thanks for your help.

A big Newbie :)




Demo-DGVDataTable.psf
$FormEvent_Load={
$dt = New-Object System.Data.DataTable
$dt.Columns.Add('FirstName', [string])
$dt.Columns.Add('LastName', [string])
$dt.Columns.Add('Age', [int])

$r = $dt.NewRow()
$r['firstname'] = 'Joe'
$r['lastname'] = 'smith'
$r['age'] = 45

$dt.Rows.Add($r)
$datagridview1.DataSource=$dt
}

$buttonExport_Click = {
$savefiledialog1.AddExtension='xml'
if ('Ok' -eq $savefiledialog1.ShowDialog()) {
$savexml = $savefiledialog1.FileName
$datagridview1.Rows | select -expand DataBoundItem | Export-Clixml -Path $savexml
}
}

$buttonNewPerson_Click={
$dr = $datagridview1.DataSource.NewRow()
if(.\New-PersonForm.ps1 $dr -eq 'Ok'){
$datagridview1.DataSource.Rows.Add($dr)
}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add ComboBoxColum

Post by jvierra »

Hi. Thanks for moving the post.

The code you posted is part of a PSF PowerShell Studio project. You can use the designer to add columns and bind them to data by name.

What you have posted is just the added code from the script editor. Look at the blog posts showing how to manage the grid control. They are a good place to start learning how to use forms.

https://www.sapien.com/blog/topics/user ... istrators/
User avatar
DanyGr
Posts: 5
Last visit: Tue Apr 06, 2021 8:07 am

Re: Add ComboBoxColum

Post by DanyGr »

Hi,
I have add a ComboBoxColumn but when I click export the ComboBox value does not appear in the xml file. Also how can I put this column the last one.

Do you think you could modify the example file to show me how can I do that?

Thanks again.
Attachments
Demo-DGVDataTable.psf
(48.13 KiB) Downloaded 179 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add ComboBoxColum

Post by jvierra »

If you use a combo box that is for a field you must bind the data name to the field name then it will connect.
User avatar
DanyGr
Posts: 5
Last visit: Tue Apr 06, 2021 8:07 am

Re: Add ComboBoxColum

Post by DanyGr »

I'm still searching :?

I have this error ""System.ArgumentException: DataGridViewComboBoxCell value is not valid."
when I run this command "$dataGridView1.Rows.Add($row.name, $row.fullname, $row.extension)"

How can I load data to DataGridViewComboBoxCell?

Thanks



Here is my sample code:
#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: e49f46f8-600b-4189-8308-2909329c646a
# Source File: C:\Users\dany\Documents\SAPIEN\PowerShell Studio\Files\Test1.psf
#------------------------------------------------------------------------

<#
.NOTES
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
.DESCRIPTION
GUI script generated by PowerShell Studio 2016
#>
#----------------------------------------------
#region Application Functions
#----------------------------------------------

#endregion Application Functions

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-Test1_psf {

#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects

#----------------------------------------------
# User Generated Script
#----------------------------------------------

#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
$Col1 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
$Col2 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
$Col3 = New-Object 'System.Windows.Forms.DataGridViewComboBoxColumn'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($datagridview1)
$form1.ClientSize = '622, 261'
$form1.Name = 'form1'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# datagridview1
#
$datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
[void]$datagridview1.Columns.Add($Col1)
[void]$datagridview1.Columns.Add($Col2)
[void]$datagridview1.Columns.Add($Col3)
$datagridview1.Location = '42, 32'
$datagridview1.Name = 'datagridview1'
$datagridview1.Size = '513, 197'
$datagridview1.TabIndex = 0
$datagridview1.add_CellContentClick($datagridview1_CellContentClick)
$datagridview1.EditMode= "EditOnEnter"

$files = Get-ChildItem c:\windows -File | select name, fullname, extension
$rows = @($files)
foreach ($row in $rows)
{
$dataGridView1.Rows.Add($row.name, $row.fullname, $row.extension)
}

#
# Col1
#
$Col1.HeaderText = 'Col1'
$Col1.Name = 'Col1'
$Col1.ToolTipText = 'Col1'

#
# Col2
#
$Col2.HeaderText = 'Col2'
$Col2.Name = 'Col2'
$Col2.ReadOnly = $True
#
# Col3
#
$Col3.HeaderText = 'Col3'
[void]$Col3.Items.Add('1')
[void]$Col3.Items.Add('2')
[void]$Col3.Items.Add('3')
[void]$Col3.Items.Add('4')
[void]$Col3.Items.Add('5')
$Col3.Name = 'Col3'

$form1.ResumeLayout()
#endregion Generated Form Code

#----------------------------------------------


#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()



$form1_Load={
#TODO: Initialize Form Controls here
# $files = Get-ChildItem c:\windows -File | select name, extension, fullname


}


#region Control Helper Functions
function Load-DataGridView
{
<#
.SYNOPSIS
This functions helps you load items into a DataGridView.

.DESCRIPTION
Use this function to dynamically load items into the DataGridView control.

.PARAMETER DataGridView
The DataGridView control you want to add items to.

.PARAMETER Item
The object or objects you wish to load into the DataGridView's items collection.

.PARAMETER DataMember
Sets the name of the list or table in the data source for which the DataGridView is displaying data.

#>
Param (
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.DataGridView]$DataGridView,
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
$Item,
[Parameter(Mandatory=$false)]
[string]$DataMember
)
$DataGridView.SuspendLayout()
$DataGridView.DataMember = $DataMember

if ($Item -is [System.ComponentModel.IListSource]`
-or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
{
$DataGridView.DataSource = $Item
}
else
{
$array = New-Object System.Collections.ArrayList

if ($Item -is [System.Collections.IList])
{
$array.AddRange($Item)
}
else
{
$array.Add($Item)
}
$DataGridView.DataSource = $array
}

$DataGridView.ResumeLayout()
}

function ConvertTo-DataTable
{
<#
.SYNOPSIS
Converts objects into a DataTable.

.DESCRIPTION
Converts objects into a DataTable, which are used for DataBinding.

.PARAMETER InputObject
The input to convert into a DataTable.

.PARAMETER Table
The DataTable you wish to load the input into.

.PARAMETER RetainColumns
This switch tells the function to keep the DataTable's existing columns.

.PARAMETER FilterWMIProperties
This switch removes WMI properties that start with an underline.

.EXAMPLE
$DataTable = ConvertTo-DataTable -InputObject (Get-Process)
#>
[OutputType([System.Data.DataTable])]
param(
[ValidateNotNull()]
$InputObject,
[ValidateNotNull()]
[System.Data.DataTable]$Table,
[switch]$RetainColumns,
[switch]$FilterWMIProperties)

if($Table -eq $null)
{
$Table = New-Object System.Data.DataTable
}

if($InputObject-is [System.Data.DataTable])
{
$Table = $InputObject
}
else
{
if(-not $RetainColumns -or $Table.Columns.Count -eq 0)
{
#Clear out the Table Contents
$Table.Clear()

if($InputObject -eq $null){ return } #Empty Data

$object = $null
#find the first non null value
foreach($item in $InputObject)
{
if($item -ne $null)
{
$object = $item
break
}
}

if($object -eq $null) { return } #All null then empty

#Get all the properties in order to create the columns
foreach ($prop in $object.PSObject.Get_Properties())
{
if(-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__'))#filter out WMI properties
{
#Get the type from the Definition string
$type = $null

if($prop.Value -ne $null)
{
try{ $type = $prop.Value.GetType() } catch {}
}

if($type -ne $null) # -and [System.Type]::GetTypeCode($type) -ne 'Object')
{
[void]$table.Columns.Add($prop.Name, $type)
}
else #Type info not found
{
[void]$table.Columns.Add($prop.Name)
}
}
}

if($object -is [System.Data.DataRow])
{
foreach($item in $InputObject)
{
$Table.Rows.Add($item)
}
return @(,$Table)
}
}
else
{
$Table.Rows.Clear()
}

foreach($item in $InputObject)
{
$row = $table.NewRow()

if($item)
{
foreach ($prop in $item.PSObject.Get_Properties())
{
if($table.Columns.Contains($prop.Name))
{
$row.Item($prop.Name) = $prop.Value
}
}
}
[void]$table.Rows.Add($row)
}
}

return @(,$Table)
}
#endregion

$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#TODO: Place custom script here


}

# --End User Generated Script--
#----------------------------------------------
#region Generated Events
#----------------------------------------------

$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}

$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$datagridview1.remove_CellContentClick($datagridview1_CellContentClick)
$form1.remove_Load($form1_Load)
$form1.remove_Load($Form_StateCorrection_Load)
$form1.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
#endregion Generated Events

#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($datagridview1)
$form1.ClientSize = '622, 261'
$form1.Name = 'form1'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# datagridview1
#
$datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
$datagridview1.Location = '42, 32'
$datagridview1.Name = 'datagridview1'
$datagridview1.Size = '513, 197'
$datagridview1.TabIndex = 0
$datagridview1.add_CellContentClick($datagridview1_CellContentClick)
$form1.ResumeLayout()
#endregion Generated Form Code

#----------------------------------------------

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()

} #End Function

#Call the form
Call-Test1_psf | Out-Null
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: Add ComboBoxColum

Post by monoeagle »

It's better to upload the psf file instead of posting it inline without Code Tags.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add ComboBoxColum

Post by jvierra »

You can add items to the combobox in many ways. Here I sone:

$datagridview1.Columns['age'].Items.AddRange(@([int]45,[int]46,[int]47,[int]48))
User avatar
DanyGr
Posts: 5
Last visit: Tue Apr 06, 2021 8:07 am

Re: Add ComboBoxColum

Post by DanyGr »

Thanks for your help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Add ComboBoxColum

Post by jvierra »

DanyGr wrote:Thanks for your help!
You are welcome.

Is it working now?
User avatar
DanyGr
Posts: 5
Last visit: Tue Apr 06, 2021 8:07 am

Re: Add ComboBoxColum

Post by DanyGr »

almost yes! :)
This topic is 7 years and 9 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