I'm using the grid search form and I'm populating the datagridview from a csv-file using the ConvertTo-DataTable and Update-DataGridView helper-functions. They works fine, but I want one of the columns to be a DataGridViewComboBoxColumn (ideally I'd like it to be a mix between combobox and textbox, but it seems to be either impossible or a lot of work?).
I've tried to add the columns in the designer beforehand and changed one column to be a combobox, but when I run the script, new columns are added with exactly the same names as the headers (they are also exactly the same name as DataPropertyName).
Will I have to add the data without the use of Update-DataGridView to get this to work?
Adding combobox column to DataGridView
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.
- MattTrakker
- Posts: 5
- Last visit: Mon Dec 05, 2022 4:16 am
Re: Adding combobox column to DataGridView
To bind a CSV to a grid the best thing is to not pre-define columns. The CSV will be bound by name.
$datagridview1.DataSource = ConvertTo-DataTable $csv
Creating a ComboBox column is not possible when loading but can be added after the load by creating a new column. You must supply the contents of the ComboBox for it to work. The ComboBox contents can be added from a DataSource or from an array of items (strings). The list must match the items in the CV field.
$datagridview1.DataSource = ConvertTo-DataTable $csv
Creating a ComboBox column is not possible when loading but can be added after the load by creating a new column. You must supply the contents of the ComboBox for it to work. The ComboBox contents can be added from a DataSource or from an array of items (strings). The list must match the items in the CV field.
- MattTrakker
- Posts: 5
- Last visit: Mon Dec 05, 2022 4:16 am
Re: Adding combobox column to DataGridView
Thanks for your input!
I managed to add the column, but I'm having a hard time populating the combobox with data.
The code below is an excerpt from my script and it gives me a "Cannot index into a null array" error.
What is the correct syntax for adding elements into the combobox?
I managed to add the column, but I'm having a hard time populating the combobox with data.
The code below is an excerpt from my script and it gives me a "Cannot index into a null array" error.
What is the correct syntax for adding elements into the combobox?
Code: Select all
$Results = Import-Csv Inputdata.csv
$results = ConvertTo-DataTable -InputObject $results -FilterWMIProperties
Update-DataGridView -DataGridView $datagridviewResults -Item $results
$ColumnCombobox = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$datagridviewResults.Columns.Add($ColumnCombobox, 'Combobox')
$items = @("Item1", "item2", "item3")
for ($i = 0; $i -lt $datagridviewResults.RowCount; $i++) {
$datagridviewResults.Rows[$i].Columns['Combobox'].Items.AddRange($items)
}
Re: Adding combobox column to DataGridView
Her eis an example of how to do this with pre-defined columns. With undefined columns you will have to programmatically add the combo and populate it.
- Attachments
-
- Demo-DGVComboBox.psf
- (25.16 KiB) Downloaded 308 times
- MattTrakker
- Posts: 5
- Last visit: Mon Dec 05, 2022 4:16 am
Re: Adding combobox column to DataGridView
Thanks for the Example. I feel like I'm on to something, but could you elaborate a bit of how to populate it programmatically?
What is wrong in the code-snippet I posted?
You code snippet is trying to populate every row's CB. We just want to set the column and add the items to the column definition of the combobox once before the grid is populated.
What is wrong in the code-snippet I posted?
You code snippet is trying to populate every row's CB. We just want to set the column and add the items to the column definition of the combobox once before the grid is populated.
- MattTrakker
- Posts: 5
- Last visit: Mon Dec 05, 2022 4:16 am
Re: Adding combobox column to DataGridView
That example didn't help me, but I figured out what I was doing wrong.
I had to change "columns" to "cells" in my for-loop.
I had to change "columns" to "cells" in my for-loop.
Code: Select all
$items = @("Item1", "item2", "item3")
for ($i = 0; $i -lt $datagridviewResults.RowCount; $i++) {
$datagridviewResults.Rows[$i].Cells['Combobox'].Items.AddRange($items)
}
- MattTrakker
- Posts: 5
- Last visit: Mon Dec 05, 2022 4:16 am
Re: Adding combobox column to DataGridView
Lets say the items that go into the combobox comes from a function and are something new each time, and I wanted to color-code the combobox based on the outcome. Would I have to add the items like in the example and go through the cells later and add the color-coding? Or is there an different (easier) way of doing it?