DataGridView Help

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
vmotto
Posts: 4
Joined: Wed Jun 06, 2012 9:40 am

DataGridView Help

Post by vmotto » Wed Jun 06, 2012 9:40 am

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

$datagridview1.Rows.Clear()
$vnics = Get-UcsServiceProfile -name $SpSelected | Get-UcsVnic
foreach ($vnic in $vnics) {
$vnic | Get-UcsVnicInterface | ForEach-Object{
$datagridview1.Rows.Add($vnic.Name,$_.Addr,$_.Name)
}
}

Thanks

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

DataGridView Help

Post by davidc » Wed Jun 06, 2012 10:29 am

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
2")
After you can add the rows:

#Fill in Rows

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

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

David
davidc2012-06-06 17:30:23
David
SAPIEN Technologies, Inc.

User avatar
vmotto
Posts: 4
Joined: Wed Jun 06, 2012 9:40 am

DataGridView Help

Post by vmotto » Thu Jun 07, 2012 2:45 am

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

$datagridview1.Rows.Add($vnic.Name,$_.Addr,$_.Name)

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.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

DataGridView Help

Post by davidc » Thu Jun 07, 2012 3:46 am

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
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

DataGridView Help

Post by davidc » Thu Jun 07, 2012 3:48 am

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
David
SAPIEN Technologies, Inc.