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.
User avatar
DanyGr
Posts: 5
Joined: Mon Apr 25, 2016 12:05 pm

Add ComboBoxColum

Post by DanyGr » Thu Jun 02, 2016 12:37 pm

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)
}
}

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Add ComboBoxColum

Post by jvierra » Thu Jun 02, 2016 1:01 pm

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
Joined: Mon Apr 25, 2016 12:05 pm

Re: Add ComboBoxColum

Post by DanyGr » Thu Jun 02, 2016 1:25 pm

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 64 times

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Add ComboBoxColum

Post by jvierra » Thu Jun 02, 2016 1:44 pm

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
Joined: Mon Apr 25, 2016 12:05 pm

Re: Add ComboBoxColum

Post by DanyGr » Sun Jun 05, 2016 7:47 pm

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: 107
Joined: Tue Oct 13, 2015 9:10 am

Re: Add ComboBoxColum

Post by monoeagle » Mon Jun 06, 2016 2:41 am

It's better to upload the psf file instead of posting it inline without Code Tags.

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Add ComboBoxColum

Post by jvierra » Mon Jun 06, 2016 4:11 am

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
Joined: Mon Apr 25, 2016 12:05 pm

Re: Add ComboBoxColum

Post by DanyGr » Sat Jun 11, 2016 7:50 pm

Thanks for your help!

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Add ComboBoxColum

Post by jvierra » Sat Jun 11, 2016 8:50 pm

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

Is it working now?

User avatar
DanyGr
Posts: 5
Joined: Mon Apr 25, 2016 12:05 pm

Re: Add ComboBoxColum

Post by DanyGr » Sat Jun 11, 2016 9:10 pm

almost yes! :)

Locked