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.
Locked
User avatar
MattTrakker
Posts: 5
Joined: Mon May 26, 2014 1:19 am

Adding combobox column to DataGridView

Post by MattTrakker » Fri Jul 06, 2018 5:11 am

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?

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

Re: Adding combobox column to DataGridView

Post by jvierra » Fri Jul 06, 2018 10:33 am

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
Joined: Mon May 26, 2014 1:19 am

Re: Adding combobox column to DataGridView

Post by MattTrakker » Mon Jul 09, 2018 3:44 am

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

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

Re: Adding combobox column to DataGridView

Post by jvierra » Mon Jul 09, 2018 4:00 am

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

User avatar
MattTrakker
Posts: 5
Joined: Mon May 26, 2014 1:19 am

Re: Adding combobox column to DataGridView

Post by MattTrakker » Mon Jul 09, 2018 4:34 am

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.

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

Re: Adding combobox column to DataGridView

Post by jvierra » Mon Jul 09, 2018 4:53 am

Look at the example. It shows exactly how to do it.

User avatar
MattTrakker
Posts: 5
Joined: Mon May 26, 2014 1:19 am

Re: Adding combobox column to DataGridView

Post by MattTrakker » Wed Jul 11, 2018 12:22 am

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

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

Re: Adding combobox column to DataGridView

Post by jvierra » Wed Jul 11, 2018 12:26 am

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
Joined: Mon May 26, 2014 1:19 am

Re: Adding combobox column to DataGridView

Post by MattTrakker » Wed Jul 11, 2018 5:24 am

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?

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

Re: Adding combobox column to DataGridView

Post by jvierra » Wed Jul 11, 2018 6:06 am

The paint method will set the colors as needed. You just have to write the code that chooses the colors.

Locked