DefaultCellStyle should not be shared between DataGridViews

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 6 years and 2 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DefaultCellStyle should not be shared between DataGridViews

Post by jvierra »

I would just change all cell backgrounds for the selected DGV. Changing the template will cause issues. What you are trying to do is not really part of the design of the DGV. A simpler approach might be better.
You also need to identify what "selected" means. Detecting "control selected" is called "focus".

The issue I see is that the PSS copies the changes made in code into the PSF file. Once that is copied in the grid then fails to work as expected. This would not happen if you were not changing the default grid cell styles.

THe following two lines should not be copied into the PSF (in bold)

$dgvOwner.ColumnHeadersHeightSizeMode = 'AutoSize'
$dgvOwner.Location = '13, 33'
$dgvOwner.Margin = '4, 4, 4, 4'
$dgvOwner.Name = 'dgvOwner'
$dgvOwner.RowTemplate.DefaultCellStyle.SelectionBackColor = 'White'
$dgvOwner.RowTemplate.DefaultCellStyle.SelectionForeColor = 'Black'
$dgvOwner.RowTemplate.Height = 24
$dgvOwner.Size = '320, 534'
$dgvOwner.TabIndex = 1
$dgvOwner.add_Enter($dgvOwner_Enter)
$dgvOwner.add_Leave($dgvOwner_Leave)

They only get coped for the first DGV but not the second. This is a Sapien bug. Once these lines are in the PSF the control is forced to this default. Removing these lines restores the control to normal behavior.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DefaultCellStyle should not be shared between DataGridViews

Post by jvierra »

The example is from my DGV test. It happens with all templates and the properties cannot be set back to default to remove the lines.

Setting defaults at any place can cause the control to misbehave. As mentioned above this may be a Net bug. I have never seen it before but I would never try to dynamically change the template. I always just use row colors or draw a highlight around the focused control.

If your workaround satisfies you good. Just remember that other things may act oddly so don't forget you patched around this.

Good luck.
User avatar
WimSKW
Posts: 8
Last visit: Mon Jul 27, 2020 2:16 am

Re: DefaultCellStyle should not be shared between DataGridViews

Post by WimSKW »

Thank you for looking into this.
Yes, the workaround works fine and I already wrote a comment in my code about the issue.

I started the topic for 2 reasons: To bring the issue to the attention of Sapien and to get confirmation on what I found is really a bug or not.
Your saying "This is a Sapien bug" confirms it.
Sapien is also looking into it, so I hope it will get fixed one day or another.

FYI, this morning I upgraded to PowerShell Studio 2018 v5.5.148 and I can confirm that the issue is exactly the same in the 2018 version.

Regards,
-=Wim=-
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DefaultCellStyle should not be shared between DataGridViews

Post by jvierra »

No. I am not sure that it is a Sapien bug. Most properties in the property grid can be set back to default and they are removed from the script. THe templates are not removed. This may just be the behavior of the property grid for that type of control. Sapien code just reacts to change and generates the code when we change a property.

This needs further investigation. Any bug may actually be in how the net Framework handles the properties and it may not be a bug as dynamically altering these templates is not a normal thing in a DGV or in other complex controls. In C# we would just generate the control once and use other methods to show the control has focus.

In WPF we can just tell a control to display a "halo" when it has focus. I would just draw a bright line around the focused control. I suspect that would be easier.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DefaultCellStyle should not be shared between DataGridViews

Post by jvierra »

I cleaned out the PSF file manually and now the transfer highlighting works as expected.

See attached:
Attachments
Test-DGVIssue.psf
(16.44 KiB) Downloaded 102 times
User avatar
WimSKW
Posts: 8
Last visit: Mon Jul 27, 2020 2:16 am

Re: DefaultCellStyle should not be shared between DataGridViews

Post by WimSKW »

What you show in your psf, is exactly what I described in "Workaround 2", except that I initialize the DefaultCellStyle of each DGV in code.
However, you don't need to clean the psf manually. The properties in the DGV property grid can be set back to default just by selecting them (the ellipsis button will show) and then hitting DEL.
So basically, it comes down to this:
  • With default (DefaultCellStyle-)properties in the property grid, PowerShell Studio doesn't generate code; you have to initialize the settings in code yourself, which works (see "Workaround 2").
  • If you do initialize the properties in the property grid, PowerShell Studio does generate code:
    • With different properties for both DGVs, PowerShell Studio generates two objects; one for each DGV and the generated code works (see "Workaround 1").
    • With identical properties for both DGVs, PowerShell Studio only generates one object that is assigned to (and hence shared by) both DGVs, and the generated code doesn't work (see "Problem"). IMHO, this is a Sapien bug.
Regards,
-=Wim=-
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DefaultCellStyle should not be shared between DataGridViews

Post by jvierra »

But that does not remove them from the PSF file when I do that. As long as they are in the PSF file the DGV will not behave as needed.

Setting the initial in the load event changes nothing. The enter/leave events set the controls correctly. As soon as you try to also make those settings in the property grid the controls stop behaving correctly which is what I think may be broken in the Net Framework.

I didn't try building as Net 2 only to see if it is a new issue.

The code you posted had those settings in the PSF file.
User avatar
WimSKW
Posts: 8
Last visit: Mon Jul 27, 2020 2:16 am

Re: DefaultCellStyle should not be shared between DataGridViews

Post by WimSKW »

jvierra wrote: Fri Feb 09, 2018 12:12 am But that does not remove them from the PSF file when I do that.
We must be doing something different then.
This is an extract of the psf file after I set the DefaultCellStyle properties in the property grid. You see that <Property name="DefaultCellStyle"> is present for both DataGridView objects:

Code: Select all

<Object type="System.Windows.Forms.DataGridView, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="dgvEquipment" children="Controls">
  <Property name="ColumnHeadersHeightSizeMode">AutoSize</Property>
  <Property name="DefaultCellStyle">
    <InstanceDescriptor member="AAEAAAD/////AQAAAAAAAAAEAQAAAC9TeXN0ZW0uUmVmbGVjdGlvbi5NZW1iZXJJbmZvU2VyaWFsaXphdGlvbkhvbGRlcgcAAAAETmFtZQxBc3NlbWJseU5hbWUJQ2xhc3NOYW1lCVNpZ25hdHVyZQpTaWduYXR1cmUyCk1lbWJlclR5cGUQR2VuZXJpY0FyZ3VtZW50cwEBAQEBAAMIDVN5c3RlbS5UeXBlW10GAgAAAAUuY3RvcgYDAAAAV1N5c3RlbS5XaW5kb3dzLkZvcm1zLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OQYEAAAAKlN5c3RlbS5XaW5kb3dzLkZvcm1zLkRhdGFHcmlkVmlld0NlbGxTdHlsZQYFAAAADFZvaWQgLmN0b3IoKQYGAAAABy5jdG9yKCkBAAAACgs=">
      <Property name="Alignment">MiddleLeft</Property>
      <Property name="BackColor">Window</Property>
      <Property name="Font">Microsoft Sans Serif, 8.25pt</Property>
      <Property name="ForeColor">ControlText</Property>
      <Property name="SelectionBackColor">InactiveCaption</Property>
      <Property name="SelectionForeColor">ControlText</Property>
      <Property name="WrapMode">False</Property>
    </InstanceDescriptor>
  </Property>
  <Property name="Location">264, 23</Property>
  <Property name="MultiSelect">False</Property>
  <Property name="Name">dgvEquipment</Property>
  <Property name="Size">240, 401</Property>
  <Property name="TabIndex">1</Property>
  <Event name="Enter">dgvEquipment_Enter</Event>
  <Event name="Leave">dgvEquipment_Leave</Event>
</Object>
<Object type="System.Windows.Forms.DataGridView, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="dgvOwner" children="Controls">
  <Property name="ColumnHeadersHeightSizeMode">AutoSize</Property>
  <Property name="DefaultCellStyle">
    <InstanceDescriptor member="AAEAAAD/////AQAAAAAAAAAEAQAAAC9TeXN0ZW0uUmVmbGVjdGlvbi5NZW1iZXJJbmZvU2VyaWFsaXphdGlvbkhvbGRlcgcAAAAETmFtZQxBc3NlbWJseU5hbWUJQ2xhc3NOYW1lCVNpZ25hdHVyZQpTaWduYXR1cmUyCk1lbWJlclR5cGUQR2VuZXJpY0FyZ3VtZW50cwEBAQEBAAMIDVN5c3RlbS5UeXBlW10GAgAAAAUuY3RvcgYDAAAAV1N5c3RlbS5XaW5kb3dzLkZvcm1zLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OQYEAAAAKlN5c3RlbS5XaW5kb3dzLkZvcm1zLkRhdGFHcmlkVmlld0NlbGxTdHlsZQYFAAAADFZvaWQgLmN0b3IoKQYGAAAABy5jdG9yKCkBAAAACgs=">
      <Property name="Alignment">MiddleLeft</Property>
      <Property name="BackColor">Window</Property>
      <Property name="Font">Microsoft Sans Serif, 8.25pt</Property>
      <Property name="ForeColor">ControlText</Property>
      <Property name="SelectionBackColor">InactiveCaption</Property>
      <Property name="SelectionForeColor">ControlText</Property>
      <Property name="WrapMode">False</Property>
    </InstanceDescriptor>
  </Property>
  <Property name="Location">12, 23</Property>
  <Property name="MultiSelect">False</Property>
  <Property name="Name">dgvOwner</Property>
  <Property name="Size">240, 401</Property>
  <Property name="TabIndex">0</Property>
  <Event name="Enter">dgvOwner_Enter</Event>
  <Event name="Leave">dgvOwner_Leave</Event>
</Object>
Then I removed the properties by selecting DefaultCellStyle, hitting DEL and saving the file. Here's what the psf file looked like:

Code: Select all

<Object type="System.Windows.Forms.DataGridView, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="dgvEquipment" children="Controls">
  <Property name="ColumnHeadersHeightSizeMode">AutoSize</Property>
  <Property name="Location">264, 23</Property>
  <Property name="MultiSelect">False</Property>
  <Property name="Name">dgvEquipment</Property>
  <Property name="Size">240, 401</Property>
  <Property name="TabIndex">1</Property>
  <Event name="Enter">dgvEquipment_Enter</Event>
  <Event name="Leave">dgvEquipment_Leave</Event>
</Object>
<Object type="System.Windows.Forms.DataGridView, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="dgvOwner" children="Controls">
  <Property name="ColumnHeadersHeightSizeMode">AutoSize</Property>
  <Property name="Location">12, 23</Property>
  <Property name="MultiSelect">False</Property>
  <Property name="Name">dgvOwner</Property>
  <Property name="Size">240, 401</Property>
  <Property name="TabIndex">0</Property>
  <Event name="Enter">dgvOwner_Enter</Event>
  <Event name="Leave">dgvOwner_Leave</Event>
</Object>
There's no sign of <Property name="DefaultCellStyle"> in the psf file anymore. For me this proves that you can "reset" the property to the default by hitting DEL on the property.

I think there's a misunderstanding about what I mean by "generated code". It's not the psf file I'm talking about. The psf file contains all the form elements and code. The generated code I'm talking about is in the (temporary) file that PowerShell Studio creates when you run the project.
Here's an extract of what is generated in the "problem" scenario. It's found in "DGV_Demo.Run.ps1" under #region Generated Form Code:

Code: Select all

#
# dgvEquipment
#
$dgvEquipment.ColumnHeadersHeightSizeMode = 'AutoSize'
$System_Windows_Forms_DataGridViewCellStyle_1 = New-Object 'System.Windows.Forms.DataGridViewCellStyle'
$System_Windows_Forms_DataGridViewCellStyle_1.Alignment = 'MiddleLeft'
$System_Windows_Forms_DataGridViewCellStyle_1.BackColor = 'Window'
$System_Windows_Forms_DataGridViewCellStyle_1.Font = 'Microsoft Sans Serif, 8.25pt'
$System_Windows_Forms_DataGridViewCellStyle_1.ForeColor = 'ControlText'
$System_Windows_Forms_DataGridViewCellStyle_1.SelectionBackColor = 'InactiveCaption'
$System_Windows_Forms_DataGridViewCellStyle_1.SelectionForeColor = 'ControlText'
$System_Windows_Forms_DataGridViewCellStyle_1.WrapMode = 'False'
$dgvEquipment.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1
$dgvEquipment.Location = '264, 23'
$dgvEquipment.MultiSelect = $False
$dgvEquipment.Name = 'dgvEquipment'
$dgvEquipment.Size = '240, 401'
$dgvEquipment.TabIndex = 1
$dgvEquipment.add_Enter($dgvEquipment_Enter)
$dgvEquipment.add_Leave($dgvEquipment_Leave)
#
# dgvOwner
#
$dgvOwner.ColumnHeadersHeightSizeMode = 'AutoSize'
$dgvOwner.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1
$dgvOwner.Location = '12, 23'
$dgvOwner.MultiSelect = $False
$dgvOwner.Name = 'dgvOwner'
$dgvOwner.Size = '240, 401'
$dgvOwner.TabIndex = 0
$dgvOwner.add_Enter($dgvOwner_Enter)
$dgvOwner.add_Leave($dgvOwner_Leave)
$dgvOwner.EndInit()
$dgvEquipment.EndInit()
$frmDGV_Demo.ResumeLayout()
You see, for the $dgvEquipment, it generates an object "$System_Windows_Forms_DataGridViewCellStyle_1" and then sets $dgvEquipment.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1.
For $dgvOwner, it doesn't generate an object but sets $dgvOwner.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1. So both $dgvEquipment as $dgvOwner are linked to $System_Windows_Forms_DataGridViewCellStyle_1.

In the "Workaround 1" scenario, the generated code looks different:

Code: Select all

#
# dgvEquipment
#
$dgvEquipment.ColumnHeadersHeightSizeMode = 'AutoSize'
$System_Windows_Forms_DataGridViewCellStyle_1 = New-Object 'System.Windows.Forms.DataGridViewCellStyle'
$System_Windows_Forms_DataGridViewCellStyle_1.Alignment = 'MiddleLeft'
$System_Windows_Forms_DataGridViewCellStyle_1.BackColor = 'Window'
$System_Windows_Forms_DataGridViewCellStyle_1.Font = 'Microsoft Sans Serif, 8.25pt'
$System_Windows_Forms_DataGridViewCellStyle_1.ForeColor = 'ControlText'
$System_Windows_Forms_DataGridViewCellStyle_1.SelectionBackColor = 'InactiveCaption'
$System_Windows_Forms_DataGridViewCellStyle_1.SelectionForeColor = 'ControlText'
$System_Windows_Forms_DataGridViewCellStyle_1.WrapMode = 'False'
$dgvEquipment.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1
$dgvEquipment.Location = '264, 23'
$dgvEquipment.MultiSelect = $False
$dgvEquipment.Name = 'dgvEquipment'
$dgvEquipment.Size = '240, 401'
$dgvEquipment.TabIndex = 1
$dgvEquipment.add_Enter($dgvEquipment_Enter)
$dgvEquipment.add_Leave($dgvEquipment_Leave)
#
# dgvOwner
#
$dgvOwner.ColumnHeadersHeightSizeMode = 'AutoSize'
$System_Windows_Forms_DataGridViewCellStyle_2 = New-Object 'System.Windows.Forms.DataGridViewCellStyle'
$System_Windows_Forms_DataGridViewCellStyle_2.Alignment = 'MiddleLeft'
$System_Windows_Forms_DataGridViewCellStyle_2.BackColor = 'Window'
$System_Windows_Forms_DataGridViewCellStyle_2.Font = 'Microsoft Sans Serif, 8.25pt'
$System_Windows_Forms_DataGridViewCellStyle_2.ForeColor = 'ControlText'
$System_Windows_Forms_DataGridViewCellStyle_2.SelectionBackColor = 'InactiveCaption'
$System_Windows_Forms_DataGridViewCellStyle_2.SelectionForeColor = 'Black'
$System_Windows_Forms_DataGridViewCellStyle_2.WrapMode = 'False'
$dgvOwner.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_2
$dgvOwner.Location = '12, 23'
$dgvOwner.MultiSelect = $False
$dgvOwner.Name = 'dgvOwner'
$dgvOwner.Size = '240, 401'
$dgvOwner.TabIndex = 0
$dgvOwner.add_Enter($dgvOwner_Enter)
$dgvOwner.add_Leave($dgvOwner_Leave)
$dgvOwner.EndInit()
$dgvEquipment.EndInit()
Here an object is generated for each DGV. We have $System_Windows_Forms_DataGridViewCellStyle_1 as well as $System_Windows_Forms_DataGridViewCellStyle_2.

For the sake of completeness, here's the generated code for "Workaround 2" where no objects are generated:

Code: Select all

#
# dgvEquipment
#
$dgvEquipment.ColumnHeadersHeightSizeMode = 'AutoSize'
$dgvEquipment.Location = '264, 23'
$dgvEquipment.MultiSelect = $False
$dgvEquipment.Name = 'dgvEquipment'
$dgvEquipment.Size = '240, 401'
$dgvEquipment.TabIndex = 1
$dgvEquipment.add_Enter($dgvEquipment_Enter)
$dgvEquipment.add_Leave($dgvEquipment_Leave)
#
# dgvOwner
#
$dgvOwner.ColumnHeadersHeightSizeMode = 'AutoSize'
$dgvOwner.Location = '12, 23'
$dgvOwner.MultiSelect = $False
$dgvOwner.Name = 'dgvOwner'
$dgvOwner.Size = '240, 401'
$dgvOwner.TabIndex = 0
$dgvOwner.add_Enter($dgvOwner_Enter)
$dgvOwner.add_Leave($dgvOwner_Leave)
$dgvOwner.EndInit()
$dgvEquipment.EndInit()
$frmDGV_Demo.ResumeLayout()
So, again, IMHO there is a Sapien bug in the "Generated Form Code". For every DGV that has a modified DefaultCellStyle property, a new 'System.Windows.Forms.DataGridViewCellStyle' object should be generated. I understand that they wanted to "optimize" things by not generating a second object if it's identical to the first because reusing the first object is more "economical", but the consequence is that the DefaultCellStyle cannot be changed for each DGV individually. And yes, I consider that as a bug.

Regards,
-=Wim=-
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DefaultCellStyle should not be shared between DataGridViews

Post by jvierra »

That may be the case but I have not been able to see that in my tests. If the properties grid is changed and has a setting that alone causes issues with the grids. If no views are assigned in the generated code then changing the default views directly works as expected in all of my tests.

The Sapien generated views get assigned in the creation of the grids. That causes an issue. If they are both the same view then I agree, it is a Sapien issue for that but that does not explain the bad behavior of the grid when any view is generated. The grids just don't behave.

Later or tomorrow I will try yo do a more controlled set of tests.

If the two views are being merged then Sapien should fix that issue.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DefaultCellStyle should not be shared between DataGridViews

Post by jvierra »

I did a quick check of the PSF code for views on both DGVs that were identical.

This is what is in the PSF:
  1. <InstanceDescriptor member="/////AQAAAAAAAAAEAQAAAC9TeXN0ZW0uUmVmbGVjdGlvbi5NZW1iZXJJbmZvU2VyaWFsaXphdGlvbkhvbGRlcgcAAAAETmFtZQxBc3NlbWJseU5hbWUJQ2xhc3NOYW1lCVNpZ25hdHVyZQpTaWduYXR1cmUyCk1lbWJlclR5cGUQR2VuZXJpY0FyZ3VtZW50cwEBAQEBAAMIDVN5c3RlbS5UeXBlW10GAgAAAAUuY3RvcgYDAAAAV1N5c3RlbS5XaW5kb3dzLkZvcm1zLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OQYEAAAAKlN5c3RlbS5XaW5kb3dzLkZvcm1zLkRhdGFHcmlkVmlld0NlbGxTdHlsZQYFAAAADFZvaWQgLmN0b3IoKQYGAAAABy5jdG9yKCkBAAAACgs=">
  2. <InstanceDescriptor member="AAEAAAD/////AQAAAAAAAAAEAQAAAC9TeXN0ZW0uUmVmbGVjdGlvbi5NZW1iZXJJbmZvU2VyaWFsaXphdGlvbkhvbGRlcgcAAAAETmFtZQxBc3NlbWJseU5hbWUJQ2xhc3NOYW1lCVNpZ25hdHVyZQpTaWduYXR1cmUyCk1lbWJlclR5cGUQR2VuZXJpY0FyZ3VtZW50cwEBAQEBAAMIDVN5c3RlbS5UeXBlW10GAgAAAAUuY3RvcgYDAAAAV1N5c3RlbS5XaW5kb3dzLkZvcm1zLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OQYEAAAAKlN5c3RlbS5XaW5kb3dzLkZvcm1zLkRhdGFHcmlkVmlld0NlbGxTdHlsZQYFAAAADFZvaWQgLmN0b3IoKQYGAAAABy5jdG9yKCkBAAAACgs=">
They are not identical as the second starts with "AAEAAAD" but the remainder of the ID is identical. The remainder appears to be byte encoded code.

I would think that this would generate two distinct views but, as you have pointed out, it generates only one view and applies it to both controls.

That should be easy to fix...maybe?
This topic is 6 years and 2 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