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.
User avatar
angelofstealth
Posts: 67
Joined: Wed Feb 13, 2013 4:26 pm

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: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 67
Joined: Wed Feb 13, 2013 4:26 pm

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: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 67
Joined: Wed Feb 13, 2013 4:26 pm

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: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 67
Joined: Wed Feb 13, 2013 4:26 pm

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: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 67
Joined: Wed Feb 13, 2013 4:26 pm

Re: Datagridview - DataGridViewImageColumn

Post by angelofstealth »

Sounds good, Thank you jvierra for your help.

Locked