Adding combobox column to DataGridView

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 5 years 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.
Locked
User avatar
MattTrakker
Posts: 5
Last visit: Mon Dec 05, 2022 4:16 am

Adding combobox column to DataGridView

Post by MattTrakker »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding combobox column to DataGridView

Post by jvierra »

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.
User avatar
MattTrakker
Posts: 5
Last visit: Mon Dec 05, 2022 4:16 am

Re: Adding combobox column to DataGridView

Post by MattTrakker »

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?

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)
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding combobox column to DataGridView

Post by jvierra »

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
User avatar
MattTrakker
Posts: 5
Last visit: Mon Dec 05, 2022 4:16 am

Re: Adding combobox column to DataGridView

Post by MattTrakker »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding combobox column to DataGridView

Post by jvierra »

Look at the example. It shows exactly how to do it.
User avatar
MattTrakker
Posts: 5
Last visit: Mon Dec 05, 2022 4:16 am

Re: Adding combobox column to DataGridView

Post by MattTrakker »

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.

Code: Select all

$items = @("Item1", "item2", "item3")
for ($i = 0; $i -lt $datagridviewResults.RowCount; $i++) {
	$datagridviewResults.Rows[$i].Cells['Combobox'].Items.AddRange($items)
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding combobox column to DataGridView

Post by jvierra »

Not necessary to do that. Just add the items to the column template as I showed. This only needs to be done once.
User avatar
MattTrakker
Posts: 5
Last visit: Mon Dec 05, 2022 4:16 am

Re: Adding combobox column to DataGridView

Post by MattTrakker »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Adding combobox column to DataGridView

Post by jvierra »

The paint method will set the colors as needed. You just have to write the code that chooses the colors.
This topic is 5 years 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.
Locked