Datagridview - formatting just stopped working.

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 5 years and 1 month 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
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Datagridview - formatting just stopped working.

Post by angelofstealth »

Loaded up my project today, ran the code/build and none of my datagridviews are applying any of formatting after loading the datasource. Anyone else ran into this issue/bug? Example of code down below, again I have changed nothing and it was working fine for weeks without issues. Tried recreating the datagridview, all datagridviews are not working that were previously. Just wondering if anyone has any ideas or things I could check why, cheers.

Code: Select all

$MemoryResultsSort = $Memory | Select-Object 'Computer Name (AD)', 'Computer Name (LOCAL)', 'Total Ram', 'Free Ram', 'Used Ram', 'Percent Ram Free' | Sort 'Computer Name (AD)', 'Computer Name (LOCAL)'

if (($MemoryResultsSort | Measure-Object).Count -eq 0)
{
   ## Leaving empty for now
} else {
   if (($MemoryResultsSort | Measure-Object).Count -eq 1)
   {
       Update-DataGridView $dgv_Memory $MemoryResultsSort
   } else {
       $dgv_Memory.DataSource = [collections.arraylist]$MemoryResultsSort
   }
   $dgv_Memory.Columns[0].Width = '250'
   $dgv_Memory.Columns[1].Width = '250'
   $dgv_Memory.Columns[2].Width = '140'
   $dgv_Memory.Columns[3].Width = '140'
   $dgv_Memory.Columns[4].Width = '140'
   $dgv_Memory.Columns[5].Width = '150'	
}
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Datagridview - formatting just stopped working.

Post by mxtrinidad »

Thanks for reporting your issue.
Can you please provide the following information?
Windows Version
Product name
Product version

We'll get back to you as soon as possible.
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - formatting just stopped working.

Post by angelofstealth »

(Windows Version:)
Edition: Windows 10 Pro , Version: 1803 , OS Build: 17134.407
(Product name:)
Sapien PowerShell Studio 2019
(Product version:)
5.6.157.0
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - formatting just stopped working.

Post by angelofstealth »

I am able to get the formatting to work by setting the columns (Collection) under MISC (setting width) and loading my data into the grid by below.

Code: Select all

$MemoryResultsSort | foreach {
   $dgv_Memory.Rows.Add($_.'Computer Name (AD)', $_. 'Computer Name (LOCAL)') | out-null
}
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Datagridview - formatting just stopped working.

Post by mxtrinidad »

Just an FYI and to give you other options.
You don't really need to add more code to handle the column width (unless is really needed).

Try the following property changes:

1. Form Properties
Font
FormBorderStyle - Sizable

2. Datagridview properties
Behavior
ColumnHeadersHeightSizeMode - AutoSize
RowHeadersWidthSizeMode - EnableResizing

Design
Locked - True

Layout
Anchor - Top, Bottom, Left, Right

Hope this help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - formatting just stopped working.

Post by jvierra »

A couple of other methods of sizing. Just set the mode for the columns. Do not define any columns in the designer.

In the designer set the "AutoSizeColumnsMode" to "All Cells".

Do not use "Update-Grid"

This loads a grid even if there is only one row.

Code: Select all

$MemoryResultsSort = $Memory | Select-Object 'Computer Name (AD)', 'Computer Name (LOCAL)', 'Total Ram', 'Free Ram', 'Used Ram', 'Percent Ram Free' | Sort-Object 'Computer Name (AD)', 'Computer Name (LOCAL)'

if ($MemoryResultsSort.Count){ # true if count > 0
    $dgv_Memory.DataSource = [collections.arraylist]@($MemoryResultsSort)
}
A better solution is this:

Code: Select all

$data = $Memory | 
    Select-Object 'Computer Name (AD)', 'Computer Name (LOCAL)', 'Total Ram', 'Free Ram', 'Used Ram', 'Percent Ram Free' | 
    Sort-Object 'Computer Name (AD)', 'Computer Name (LOCAL)'
$dgv_Memory.DataSource = ConvertTo-DataTable $data
This allows the grid to be sorted and filtered with little or no code. It also allows the use of Export-Csv on the sorted or filtered.
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - formatting just stopped working.

Post by angelofstealth »

Appreciate the quick feedback, those solutions will work for me. Except the part of controlling the width of the columns I want to control, as I don't want to leave some datagridviews looking empty on display. For example the computer objects names having a set width on all datagridviews looks clean. So using the designer is the only safe method that works to set the width it seems.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - formatting just stopped working.

Post by jvierra »

To control the width of columns in the designer and load them from DataTable or an arraylist you must assign the column data names to match the columns being loaded. Set the DAtaName of the column to the name of the column in the data.
In this case the column datanames must be:
'Computer Name (AD)',
'Computer Name (LOCAL)',
'Total Ram',
'Free Ram',
'Used Ram',
'Percent Ram Free'

This is not the column name or the Column header display name.
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - formatting just stopped working.

Post by angelofstealth »

The width set in the script seems to work now if i use the ConvertTo-DataTable/code you provided.

Code: Select all

$data = $Memory |
Select-Object 'Computer Name (AD)', 'Computer Name (LOCAL)', 'Total Ram', 'Free Ram', 'Used Ram', 'Percent Ram Free' |
Sort-Object 'Computer Name (AD)', 'Computer Name (LOCAL)'
$dgv_Memory.DataSource = ConvertTo-DataTable $data

$dgv_Memory.AlternatingRowsDefaultCellStyle.BackColor = 'LightGreen'
$dgv_Memory.Columns[2].DefaultCellStyle.Alignment = 'MiddleCenter'
$dgv_Memory.Columns[3].DefaultCellStyle.Alignment = 'MiddleCenter'
$dgv_Memory.Columns[4].DefaultCellStyle.Alignment = 'MiddleCenter'
$dgv_Memory.Columns[5].DefaultCellStyle.Alignment = 'MiddleCenter'
$dgv_Memory.Columns[0].Width = '300'
$dgv_Memory.Columns[1].Width = '300'
$dgv_Memory.Columns[2].Width = '150'
$dgv_Memory.Columns[3].Width = '150'
$dgv_Memory.Columns[4].Width = '150'
$dgv_Memory.Columns[5].Width = '160'
$dgv_Memory.AllowUserToAddRows = $false
$dgv_Memory.AllowUserToDeleteRows = $false
This topic is 5 years and 1 month 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