Page 1 of 1

DataGridView Help

Posted: Wed Jun 06, 2012 9:40 am
by vmotto
Are there any examples of using the DataGridView control with PowerShell??

I have no idea how to bind a datasource, or even what datasources can be bound to the control.

I have managed to fill the view with data, using the .Rows.Add method, but have no idea if that is correct way to do it. Additionally, I want the last column to be a ComboBox, however, I again have no idea how to fill the ComboBox for the column. I can successfully fill a "normal" ComboBox control, but I can not figure out how to fill the ControlBox that is part of the DataGrid View.

Here is how I'm currently displaying the data for the first three columns, which are TextBox cells

$vnics = Get-UcsServiceProfile -name $SpSelected | Get-UcsVnic
foreach ($vnic in $vnics) {
$vnic | Get-UcsVnicInterface | ForEach-Object{


DataGridView Help

Posted: Wed Jun 06, 2012 10:29 am
by davidc
If you want to Databind a source you can look at the Load-DataGridView helper function, which is automatically inserted into your form when you add a DataGridView. In the case where you are dealing with customized columns, you will have to manually add each row and value. As for setting the ComboBox values, you have to define them in the Column object. You can access the Column Editor in the designer and look for the Items property.In the script you can do the following:
#Fill in ComboBox

$Column1.Items.Add("option 1")$Column1.Items.Add("option
After you can add the rows:

#Fill in Rows

$datagridview1.Rows.Add("option 1", "text 1")

2", "text 2")

davidc2012-06-06 17:30:23

DataGridView Help

Posted: Thu Jun 07, 2012 2:45 am
by vmotto
Thanks for the info David.

I had already defined the columns through the Designer using "Edit Columns..." The first three columns are defined as "DataGridViewTextBoxColumn" and the forth Column Type is defined as DataGridViewComboBoxColumn".

I have no problems filling in the TextBoxColumns using


However, when I try to fill in the forth column, either with plain text, a variable (as in the other fields), or with an array, I receive the following error:

"System.ArgumentException: DataGridViewComboBoxCell value is not valid."

When I use a normal ComboBox, PowerShell Studio inserts a "Load-ComboBox" function that I can call with the name of the ComboBox ... however, here with the DataGridView, I do not know how to reference the individual Cell in order to leverage the Load-ComboBox function.

Additionally, through the Designer, when I "Edit Columns...", and select the forth column, in the properties, under Data, there is a DataSource parameter, but when I click on it, it only shows (none) ... is there anyway to assign an array to this field?? This is very confusing.

Thanks for your help.

DataGridView Help

Posted: Thu Jun 07, 2012 3:46 am
by davidc
It's hard to tell without seeing the full code or how you are populating the combo box column. You have to make sure you use the same object type when filling the combo box. For example, if you use strings to fill the combo box (column object), then make sure you use a string value when adding a row. If the value is not found, you will get that error message.The Load-ComboBox will not work in this case. Plus the DataGridView Cell only contains the selected value, whereas the DataGridViewColumn contains all the possible values. David

DataGridView Help

Posted: Thu Jun 07, 2012 3:48 am
by davidc
I recommend looking at the MSDN for help on the DataGridView. You can access website directly from the application by right clicking on the control and navigating the context menu. David