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 1 year and 8 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.
I used for a long time the function automatically added (Update-ComboBox) in a form when you create a combobox.
Since today I haven't have to used the "ValueMember" argument.
When I try to use it, it always retrieve the ValueMember of the last entry of the ComboBox.
$ListDoc=(Invoke-SqlQuery -Query"SELECT DOC_ID, DOC_APP, DOC_PATH FROM DOC ORDER BY DOC_APP"-ConnectionName $Database)|Select-Object DOC_ID, DOC_APP, DOC_PATH
I found another way to dislay the "ValueMember" attribut on your forum.
The result of the query is a hashtable.
I must create a System.Data.DataTable object.
$ComboBox_DelRowItem=(Invoke-SqlQuery -Query"SELECT DOC_ID, DOC_APP, DOC_PATH FROM DOC ORDER BY DOC_APP, DOC_PATH"-ConnectionName $Database)|Select-Object DOC_ID, DOC_APP, DOC_PATH
$DataTableDelRow=New-Object System.Data.DataTable
$DataTableDelRow.Columns.Add('DISPLAY')
$DataTableDelRow.Columns.Add('ID')
$DataTableDelRow.Rows.Add('','')
$ComboBox_DelRowItem|ForEach-Object{
$Display=($_.DOC_APP)+" : "+($_.DOC_PATH)
$Identifiant=$_.DOC_ID
$DataTableDelRow.Rows.Add($Display,$Identifiant)
}
$ComboBox_DeleteRow.DataSource =$DataTableDelRow
$ComboBox_DeleteRow.DisplayMember ='DISPLAY'
I retrieve the combobox valuemember attribut by :
$ComboBox_DeleteRow.$ComboBox_DeleteRow.SelectedItem.ID
You are absolutely wrong about having to create a table object. The SQL query returns a datatable and the CB is designed to work with DataTable objects. Hash tables are not really usable with controls as a datasource. The Update-ComboBox CmdLet internally turns a hash into a DataTable.
You cannot get good functionality in a CB using your method.
I have been using the correct method in all Windows data-enabled controls for two decades. Take my word for it, you are just boxing yourself into a corner.
You do not need to use Select-Object since the SQL returns those fields in a DT and using "Select-Object" just destroys the DataTable.
I recommend taking some time to learn how WinForms and WinForms controls work and how they are designed to enable and simplify coding with data objects. You might also learn how to use SQL to retrieve data in the form you need it to be used in. These things will simplify your code and it will also reduce the amount of code you write by more than a factor of ten.
This topic is 1 year and 8 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.