DataGridview Combobox error

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 1 year and 11 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
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

DataGridview Combobox error

Post by sekou2331 »

Hi,

I have created a Datagridview with columns that are combo boxes. Each column dependent on each other for data in the row. for example. When populate column 0 it will then populate column 1 then what ever is in column 1 will populate column 2. Also when I click on the first row it automatically makes another row of combo boxes. Which is ok cause that is what I want. But when I fill the first row I am ok when got to fill the second row I have an issue and get the error attached. I don't know maybe the event I am using is in correct.

With that being said I don't know if I am doing this right.
  1. $datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.     #TODO: Place custom script here
  4.     #[void][System.Windows.Forms.MessageBox]::Show($datagridview1.SelectedCells[0].FormattedValue, 'You Chose')
  5.    
  6.     #TODO: Initialize Form Controls here
  7.     if ($datagridview1.SelectedCells[0].FormattedValue -eq "Choice1")
  8.     {
  9.        
  10.         $cbItems = (Function1).Source |Select-Object -Unique
  11.         ([System.Windows.Forms.DataGridViewComboBoxColumn]$datagridview1.Columns['SrcUser']).Items.Clear()
  12.         ([System.Windows.Forms.DataGridViewComboBoxColumn]$datagridview1.Columns['SrcUser']).Items.AddRange($cbItems)
  13.         $c = $datagridview1.Columns['SrcUser']
  14.     }
  15.    
  16.     if ($datagridview1.SelectedCells[0].FormattedValue -eq "Choice2")
  17.     {
  18.        
  19.         $cbItems = (Function1).Source | Select-Object -Unique
  20.         ([System.Windows.Forms.DataGridViewComboBoxColumn]$datagridview1.Columns['SrcUser']).Items.Clear()
  21.         ([System.Windows.Forms.DataGridViewComboBoxColumn]$datagridview1.Columns['SrcUser']).Items.AddRange($cbItems)
  22.         $c = $datagridview1.Columns['SrcUser']
  23.     }
  24.    
  25.     if ([string]::IsNullOrEmpty($datagridview1.SelectedCells[2].FormattedValue))
  26.     {
  27.             $cbItems = (Function2).Source
  28.         ([System.Windows.Forms.DataGridViewComboBoxColumn]$datagridview1.Columns['Version']).Items.Clear()
  29.         ([System.Windows.Forms.DataGridViewComboBoxColumn]$datagridview1.Columns['Version']).Items.AddRange($cbItems)
  30.         $c = $datagridview1.Columns['Version']
  31.        
  32.     }
  33.    
  34.    
  35. }
Attachments
Picture of Eror
Picture of Eror
2022-03-30 09_25_00-ACS Insert Tool.png (8.43 KiB) Viewed 1271 times
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview Combobox error

Post by sekou2331 »

It looks like when I go to the next row I am unable to populate it correct it will populate the one above instead of the on I am on.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridview Combobox error

Post by jvierra »

The error is clear. The underlying data type does not match the requested operation.

I recommend running in the debugger and finding the exact line and data types. Without that as a minimum the error is mostly useless. Note that any column that contains a null in the database can throw his exception.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridview Combobox error

Post by jvierra »

sekou2331 wrote: Wed Mar 30, 2022 8:14 am It looks like when I go to the next row I am unable to populate it correct it will populate the one above instead of the on I am on.
What does this have to do with your original issue? If you add multiple unrelated issues it only makes it harder to understand what you are asking.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview Combobox error

Post by sekou2331 »

Understood. So my underlying issue here is how do I populate a cell in the current row I am in from another cell in that same row?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridview Combobox error

Post by jvierra »

JUst specify the row and cell from the passed in values.

"$_.RowIndex" contains the index of the current row. Just add the column index or name that you want to address.

$datagridview1.Rows[$_.RowIndex].Cells['column3'].Value = 'whatever'
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview Combobox error

Post by sekou2331 »

When I am adding this line I am running into the attached error. I ran debug and soon as I use the Value change event the error comes and Powershell studio does not give me anything. When I kill it I get nothing as well.
2022-03-31 17_50_51-insert.png
2022-03-31 17_50_51-insert.png (11.49 KiB) Viewed 1196 times

  1.  
  2. $datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
  3. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  4.     #TODO: Place custom script here
  5.     if ($datagridview1.CurrentCell.FormattedValue -eq "Something")
  6.     {
  7.  
  8.          $datagridview1.Rows[$_.RowIndex].Cells[1].Value = array
  9.        
  10.  
  11.     }
  12.  
  13. }
  14.  
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridview Combobox error

Post by jvierra »

You are trying to assign an array to a "cell collection". An array is not a DataGridViewCellCollection type. The "array" is not a type either it is just a token that has no meaning. It is not even a variable.

I recommend starting by learning basic PowerShell instead of just guessing. When you learn PowerShell, you will learn how to read exceptions and errors. You will also learn about "types", "type coercion". You will also learn the syntax elements and the correct method of defining strings and variables as well as how to work with collections.

Note that almost any time a property of an object ends in an "s" it indicates that it is a collection of some kind. If you inspect the property in the debugger, it will tell you that it is a DataGridViewCellCollection type. Look up the type on the MS site to learn how the type is designed with examples of how it can be used.

See: https://docs.microsoft.com/en-us/dotnet ... esktop-6.0
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview Combobox error

Post by sekou2331 »

Ok I understand but even if I do the below it is the same error. Also I was using a combo box for the cell I even tired the DropDownButton which both should except an array. I am using a row of DropDownButtons so that the user can choose from an array of string for each cell. I will read what you have given me.

$datagridview1.Rows[$_.RowIndex].Cells[1].Value = 'whatever'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridview Combobox error

Post by jvierra »

Play with this for a while and maybe you will understand how this works.

If you are data bound, then you will have to change the underlying data object.
Attachments
Dem-CellValueChanged.psf
(16.87 KiB) Downloaded 53 times
This topic is 1 year and 11 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