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

Re: DataGridview Combobox error

Post by sekou2331 »

OK I am sorry but I don't know if you wanted this to have errors to start. Also I am noticing none of your columns are combo boxes. I attached what I am trying to do.

  1. ERROR: Cannot index into a null array.
  2. Dem-CellValueChanged.psf (10, 2): ERROR: At Line: 10 char: 2
  3. ERROR: +         $this.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value = '--- Ch ...
  4. ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  6. ERROR:     + FullyQualifiedErrorId : NullArray
  7. ERROR:
  8. ERROR: Cannot index into a null array.
  9. Dem-CellValueChanged.psf (10, 2): ERROR: At Line: 10 char: 2
  10. ERROR: +         $this.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value = '--- Ch ...
  11. ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  13. ERROR:     + FullyQualifiedErrorId : NullArray
  14. ERROR:
  15. ERROR: Cannot index into a null array.
  16. Dem-CellValueChanged.psf (10, 2): ERROR: At Line: 10 char: 2
  17. ERROR: +         $this.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value = '--- Ch ...
  18. ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  20. ERROR:     + FullyQualifiedErrorId : NullArray
  21. ERROR:
Attachments
MainForm.psf
ScriptExample
(15.1 KiB) Downloaded 65 times
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 »

Of course, you are getting errors. All of your comboboxes are empty. You cannot use this if the comboboxes have not been populated. You also are starting out with a new automatic row that has all nulls in it.

There is still no way to understand what you are trying to accomplish. Using CellChanged to alter a cell is not a desired method as it is altering a cell that is not stable. You need to consider designing a standard form using standard methods rather than just guessing at how you might do something.

Since we have no idea what it is you are trying to accomplish it is impossible to suggest how you might get your desired outcome.

You need to learn about forms and controls instead of just guessing. So far, every one of your guesses have been completely wrong because you have not learned controls or how the DGV is designed and intended to be used.

As an example, you cannot alter a combobox with a string that is not listed in teh items array.

Play with this and reread the docs until you get a sense of how this works.
Attachments
MainForm.psf
(14.55 KiB) Downloaded 44 times
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview Combobox error

Post by sekou2331 »

Ok will read up on it. If I am getting what you are saying. I cant add any array to the combo manually and it has to be listed in the items array already. But the attach kind of does what I want. If you look at the code you should be able to see what I am trying to do. I am trying to add two different sets of arrays to the combo box in column 2 depending on one of the choices from column 1 while continuing down the grid with different choices for each row. I will read to see what I am doing wrong. If you can help would be appreciated.
Attachments
MainForm.psf
(15.21 KiB) Downloaded 59 times
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 »

Unfortunately, that is not what the DGV is designed to do. The ComboBoxes are not designed to be loaded for each row as the control is mostly hidden. The best you can do is add to the list in the column definition then select the newly added item.

What I see is that, like most non-windows-programmers you are trying to use forms without any knowledge of how forms are designed and used. It not just a simple system like early HTML controls. WinForms are designed to be data-bound and the DGV is designed to simplify common things we use in a data form. WinForms are not free-form objects. They have a specific purpose which is to support industry standard CUA methods and rules. While you can do things with simple, passive, forms the complex controls are hard to manage when you do not have a design that matches the whole WinForms and CUA model.

This is what CUA specifies and allows: https://en.wikipedia.org/wiki/IBM_Common_User_Access

There is no documentation on MS that tells how to use WinForms with PowerShell in fact it seems that MS is trying to discourage the use of WinForms as it does not match with the new MS design to move everything into the cloud.

My recommendation is to not try to do anything with a control that isn't given as an example in the online documentation or directly accessible as an event, property or method.

Here is a fair sampler of methods that leverage what is basic in many complex controls. I wrote the blog almost 5 years ago, but it is still ok with the exceptions that WordPress keeps screwing up the code examples so download the zip for the correct code.

http://tech-comments.blogspot.com/2017/ ... forms.html
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview Combobox error

Post by sekou2331 »

Thanks for all your. I am seeing what you are saying.
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