h41msh1c0r
Goto Top

Powershell DataGridView Buttons anpassen überschreiben

Hi PS Guru's,

ich bin wieder beim DataGridView hängen geblieben.

In einigen Spalten habe ich Buttons eingefügt.

Nun wollte ich auf den Buttons Bilder einfügen(Pfeil hoch, Pfeil runter, Pfeil nach rechts).
Bisher aber ohne Erfolg. Mit Querlesen bin ich bisher nur auf C# Sourcen gestoßen.

Wie man allerdings die Buttons mit der Powershell überschreibt hab ich noch nichts gefunden.

Hat einer von euch einen Tipp? Denn nur graue Buttons sehen doof aus.

Gruß

Content-Key: 284275

Url: https://administrator.de/contentid/284275

Printed on: May 5, 2024 at 04:05 o'clock

Member: colinardo
Solution colinardo Oct 01, 2015, updated at Dec 30, 2015 at 21:25:38 (UTC)
Goto Top
Hallo Heim###er,
dazu musst du das OnPaint-Ereignis benutzen, da eine Button-Column nativ keine Bilder unterstützt. Du musst damit also das Bild über den Button zeichnen.
Hier ein Beispiel, das das Icon eines Programms als Quelle nutzt (Um ein anderes normales Bild aus dem Dateisystem zu benutzen, nimmst du stattdessen die auskommentierte Zeile 18):

4edd7a01ab58cf79d971c88bae43a4dd

function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
#endregion

#region Form Objects
$form1 = New-Object System.Windows.Forms.Form
$dgv = New-Object System.Windows.Forms.DataGridView
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Form Objects

$handler_dgv_CellPainting = 
{
    if ($_.ColumnIndex -eq 0 -and $_.RowIndex -eq 0){
        $_.Paint($_.CellBounds,[System.Windows.Forms.DataGridViewPaintParts]::All)
        # [System.Drawing.Image]$img = [System.Drawing.Image]::FromFile("C:\Ordner\bild.png")  
        $img = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\windows\system32\taskmgr.exe')  
        $_.Graphics.DrawImage($img,$_.CellBounds.Left + 5,$_.CellBounds.Top + 2,16,16)
        $_.Handled = $true
        
    }

}

$handler_form1_Load= 
{
    $dgv.Rows.Cells.Value = "Klick mich"  

}

$handler_dataGridView1_CellContentClick= 
{

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 192
$System_Drawing_Size.Width = 316
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"  
$form1.Text = "Image on DataGridView Button"  
$form1.add_Load($handler_form1_Load)

$System_Windows_Forms_DataGridViewButtonColumn_4 = New-Object System.Windows.Forms.DataGridViewButtonColumn
$System_Windows_Forms_DataGridViewButtonColumn_4.HeaderText = "Button Spalte"  
$System_Windows_Forms_DataGridViewButtonColumn_4.Name = ""  
$System_Windows_Forms_DataGridViewButtonColumn_4.Width = 100

$dgv.Columns.Add($System_Windows_Forms_DataGridViewButtonColumn_4)|Out-Null
$dgv.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$dgv.Location = $System_Drawing_Point
$dgv.Name = "dgv"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 163
$System_Drawing_Size.Width = 288
$dgv.Size = $System_Drawing_Size
$dgv.TabIndex = 0
$dgv.add_CellPainting($handler_dgv_CellPainting)
$dgv.add_CellContentClick($handler_dataGridView1_CellContentClick)

$form1.Controls.Add($dgv)

#endregion Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function
GenerateForm
Grüße Uwe
Member: H41mSh1C0R
H41mSh1C0R Oct 01, 2015 updated at 14:52:40 (UTC)
Goto Top
Hallo Uwe,

schonmal ein fettes danke an dich. Hab beim Thread erstellen schon überlegt ob ich dich direkt anschreibe. ^^

Bei deinem Beispiel bekomme ich leider einen Fehler.

Exception calling "DrawImage" with "5" argument(s): "Value cannot be null.  
Parameter name: image"  
At line:23 char:9
+         $_.Graphics.DrawImage($icon,$_.CellBounds.Left + 5,$_.CellBounds.Top + 2 ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) , MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Hierbei ist es egal ob ich das Bild aus dem Taskmanager nehme oder ein eigenes png.

Das DataGridView was ich hier vor mir habe hat beim erzeugen folgende function mitgebracht.

#region Control Helper Functions
function Load-DataGridView
{
	Param (
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		[System.Windows.Forms.DataGridView]$DataGridView,
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		$Item,
	    [Parameter(Mandatory=$false)]
		[string]$DataMember
	)
	$DataGridView.SuspendLayout()
	$DataGridView.DataMember = $DataMember
	
	if ($Item -is [System.ComponentModel.IListSource]`
	-or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
	{
		$DataGridView.DataSource = $Item
	}
	else
	{
		$array = New-Object System.Collections.ArrayList
		
		if ($Item -is [System.Collections.IList])
		{
			$array.AddRange($Item)
		}
		else
		{	
			$array.Add($Item)	
		}
		$DataGridView.DataSource = $array
	}
	
	$DataGridView.ResumeLayout()
}
#endregion

Für das Einfügen von Inhalt hab ich folgende Zeilen zum Test.

$row = @("", "nicht vorhanden", "nicht vorhanden", $arr.GetValue(2), "V18.23", "", "")  
$dataGridView1.Rows.Add($row)

Im ersten und letzten beiden Zellen ist jeweils ein Button.

Text bekomme ich auf die Buttons wenn ich statt "" z.B. "Start" einfüge, dann hab ich dort Start stehen. =)

Gruß vom Heimshicor =)

PS:

Jetzt ist erstmal Feierabend, seid früh um 6 hier und noch nix gegessen. ;(
Member: colinardo
colinardo Oct 01, 2015 updated at 15:07:00 (UTC)
Goto Top
Uups ... , da hat der Name einer Variablen nicht gestimmt die ich nachträglich noch umbenannt habe, sorry dafür. Ist korrigiert.
Member: colinardo
colinardo Dec 30, 2015 at 19:08:58 (UTC)
Goto Top
Wenns das dann war, den Beitrag bitte noch auf gelöst setzen. Merci.

Guten Rutsch.