Page 1 of 2

Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 7:59 am
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'

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 8:07 am
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.

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 8:12 am
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'

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 8:19 am
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 }
}

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 8:37 am
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.

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 8:41 am
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].

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 8:50 am
by jvierra
If you do not set the datatsource to $null the columns will not be removed. Try it.

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 8:56 am
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.

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 9:00 am
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.

Re: Datagridview - DataGridViewImageColumn

Posted: Wed Aug 16, 2017 9:03 am
by angelofstealth
Sounds good, Thank you jvierra for your help.