Datagridview - DataGridViewImageColumn

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

Datagridview - DataGridViewImageColumn

Post by angelofstealth »

Looking for assistance on how to create/code the datagridview columns design Column Type. I know how to manually set it in the Misc/Columns/(Collection) area within Sapien PowerShell Studio. I am thinking i have to reference my datagridview/column to column type [System.Windows.Forms.DataGridViewImageColumn] but i don't know how that is set or property it is on. Tried searching the forums, don't see an article of creating datagridview from scratch/coded. The reason why i am creating from scratch is i clear the datagridview/datasource. Thanks,

Currently Setting Example:
$datagridview3.Columns[0].Width = '110'
$datagridview3.Columns[1].Width = '110'
$datagridview3.Columns[2].Width = '110'
$datagridview3.Columns[2].DefaultCellStyle.Alignment = 'middlecenter'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - DataGridViewImageColumn

Post by jvierra »

Clearing the data source does not clear the formatting. Set the column auto size mode to automatically set the column width.

If you clear the data source and have no pre-defined columns then the columns WILL be cleared. A new data source will recreate the columns.

There is really not enough information to understand your issue. How you load the data is critical to the behavior of the columns collection.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - DataGridViewImageColumn

Post by jvierra »

The following creates a column and sets some properties.

Code: Select all

Add-Type -AssemblyName System.Windows.Forms
$dgv = New-Object System.Windows.Forms.DataGridView
$dgv.Columns.Add('MyColumn', 'MyHeader')
$dgv.Columns['MyColumn'].Width = 100
$dgv.Columns['MyColumn'].DefaultCellStyle.Alignment = 'middlecenter'
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - DataGridViewImageColumn

Post by angelofstealth »

Np, will explain a bit more....
I have the formatting set in the Misc/Columns/(Collection), created table names. I have a tabcontrol when clicked loads the below routine. It works the first time with no issues in regards to the formatting. If I click another tab and come back to that tab it loads the datagridview with the data but does not keep the formatting. Note this routine is being done as part of add-jobtracker in the completedscript part. If i take out the clearing datasource it is fine. Do you think i really need to clear the datasource?

Code: Select all

$datagridview3.DataSource = $null
$list2 = [system.collections.arraylist]($results.'Tabview' | select 'Name', 'Icon', 'State', 'IP Address', 'Traffic Domain')
$datagridview3.DataSource = $list2
foreach ($Row in $datagridview3.Rows)
{
  if ($Row.Cells[2].Value -eq 'ENABLED') { $Row.Cells[1].Value = $IconSuccess }
  if ($Row.Cells[2].Value -eq 'DISABLED') { $Row.Cells[1].Value = $IconError }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - DataGridViewImageColumn

Post by jvierra »

Why do you keep reloading the data?

Start by removing this line: "$datagridview3.DataSource = $null" as it is unnecessary and will remove all formatting.
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - DataGridViewImageColumn

Post by angelofstealth »

Within the routine/job tracker it connects to the nitro load balancer api and pulls the data down. So when the tabcontrol/tabpage is clicked i want it to pull down the current information and not from minutes ago or first time it was pulled. Can a column be created as [System.Windows.Forms.DataGridViewImageColumn] ? I see the add control creates as [System.Windows.Forms.DataGridViewTextBoxColumn].
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - DataGridViewImageColumn

Post by jvierra »

If you do not set the datatsource to $null the columns will not be removed. Try it.
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - DataGridViewImageColumn

Post by angelofstealth »

It does work when i don't set the datasource to $null. The clear was to make sure that the data being loaded was from current and not was there.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datagridview - DataGridViewImageColumn

Post by jvierra »

Re-assigning the data source always replaces everything. Setting it to null removes all definitions including column.

If you don't assign columns and set the columns to autosize and generate then this would not matter. Either way works.
User avatar
angelofstealth
Posts: 69
Last visit: Wed Sep 28, 2022 10:04 am

Re: Datagridview - DataGridViewImageColumn

Post by angelofstealth »

Sounds good, Thank you jvierra for your help.
This topic is 6 years and 7 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