Skip to content

DataGridView

In a ScriptoForm, the Microsoft .NET DataGridView class is a control that is used to provide a customizable and sortable table for displaying data to the user.

ScriptoForm with example DataGridView control

In the ScriptoForm PowerShell script file, a DataGridView control should be instantiated in the Controls region and then defined within the Forms script block. A DataGridView is usually paired with a matching Label control. By convention, this control pair should be nested in the $GroupBoxMain GroupBox (as displayed in the above image) and is added to it rather than the main Form.

The steps needed to implement a DataGridView control with a ScriptoForm include:

  • Instantiate a DataGridView object and an associated Label object in the Controls region.
    Suggested name format: $LabelName | $DataGridViewName
  • Assign the required and any optional properties to the ComboBox object in the Forms region.
  • Populate the DataGridView with a list of items in an appropriate event handler in the Handlers region, as needed.
  1. Instantiate a new System.Windows.Forms.DataGridView and System.Windows.Forms.Label control pair:
    Controls Region
    #region Controls
    $FormMain = New-Object -TypeName System.Windows.Forms.Form
    $GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox
    <#
    Instantiate control objects here...
    #>
    $LabelServices = New-Object -TypeName System.Windows.Forms.Label
    $DataGridViewServices = New-Object -TypeName System.Windows.Forms.DataGridView
    $ButtonRun = New-Object -TypeName System.Windows.Forms.Button
    $ButtonClose = New-Object -TypeName System.Windows.Forms.Button
    $StatusStripMain = New-Object -TypeName System.Windows.Forms.StatusStrip
    $ToolStripStatusLabelMain = New-Object -TypeName System.Windows.Forms.ToolStripStatusLabel
    #endregion
  2. Assign properties to the pair and add them to the GroupBox object:
    Forms Region | $ShowMainForm
    #region Forms
    $ShowFormMain =
    {
    $FormWidth = 330
    $FormHeight = 260
    $FormMain.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)
    $FormMain.Text = "Title"
    $FormMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    $FormMain.ClientSize = New-Object -TypeName System.Drawing.Size($FormWidth,$FormHeight)
    $FormMain.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
    $FormMain.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
    $FormMain.MaximizeBox = $false
    $FormMain.AcceptButton = $ButtonRun
    $FormMain.CancelButton = $ButtonClose
    $FormMain.Add_Shown($FormMain_Shown)
    $GroupBoxMain.Location = New-Object -TypeName System.Drawing.Point(10,5)
    $GroupBoxMain.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 20),($FormHeight - 80))
    $FormMain.Controls.Add($GroupBoxMain)
    <#
    Assign control properties and event handlers here and add to the GroupBox...
    #>
    $LabelServices.Location = New-Object -TypeName System.Drawing.Point(15,15)
    $LabelServices.AutoSize = $true
    $LabelServices.Text = "Environment:"
    $GroupBoxMain.Controls.Add($LabelServices)
    $DataGridViewServices.Location = New-Object -TypeName System.Drawing.Point(15,35)
    $DataGridViewServices.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 50),325)
    $DataGridViewServices.TabStop = $false
    $DataGridViewServices.RowTemplate.Height = 20
    $DataGridViewServices.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
    $DataGridViewServices.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
    $DataGridViewServices.RowHeadersVisible = $false
    $DataGridViewServices.AllowUserToResizeRows = $false
    $DataGridViewServices.ReadOnly = $true
    $DataGridViewServices.AllowUserToAddRows = $false
    $DataGridViewServices.AllowUserToDeleteRows = $false
    $GroupBoxMain.Controls.Add($DataGridViewServices)
    $ButtonRun.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 175),($FormHeight - 60))
    $ButtonRun.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonRun.TabIndex = 100
    $ButtonRun.Enabled = $false
    $ButtonRun.Text = "Run"
    $ButtonRun.Add_Click($ButtonRun_Click)
    $FormMain.Controls.Add($ButtonRun)
    $ButtonClose.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 85),($FormHeight - 60))
    $ButtonClose.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonClose.TabIndex = 101
    $ButtonClose.Text = "Close"
    $ButtonClose.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $FormMain.Controls.Add($ButtonClose)
    $StatusStripMain.SizingGrip = $false
    $StatusStripMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    [void]$StatusStripMain.Items.Add($ToolStripStatusLabelMain)
    $FormMain.Controls.Add($StatusStripMain)
    [void]$FormMain.ShowDialog()
    $FormMain.Dispose()
    }
    #endregion
  3. Populate the DataGridView object with a list of items:
    Handlers Region | $ButtonRun_Click
    $ButtonRun_Click =
    {
    Invoke-FormAction -Action {
    try
    {
    $Services = Get-Service -ErrorAction SilentlyContinue | Select-Object DisplayName, Status | Sort-Object DisplayName
    if ($Services)
    {
    $DataGridViewServices.ColumnCount = 2
    $DataGridViewServices.ColumnHeadersVisible = $true
    $DataGridViewServices.Columns[0].Name = "Service Name"
    $DataGridViewServices.Columns[1].Name = "Status"
    $DataGridViewServices.Rows.Clear()
    $Services | ForEach-Object {$DataGridViewServices.Rows.Add($PSItem.DisplayName, $PSItem.Status)}
    $ColumnCount = $DataGridViewServices.Columns.Count
    $LastColumnIndex = $ColumnCount - 1
    foreach ($Column in $DataGridViewServices.Columns)
    {
    if ($Column.Index -eq $ColumnCount - $LastColumnIndex) {$Column.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::Fill}
    else {$Column.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::AllCells}
    $Column.SortMode = [System.Windows.Forms.DataGridViewColumnSortMode]::NotSortable
    $Column.SortMode = [System.Windows.Forms.DataGridViewColumnSortMode]::Automatic
    }
    $FormMain.Refresh()
    }
    else
    {
    [void][System.Windows.Forms.MessageBox]::Show(
    "No services found on this computer.",
    "Information",
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Information)
    }
    }
    catch
    {
    [void][System.Windows.Forms.MessageBox]::Show(
    $PSItem.Exception.Message + "`n`nPlease contact $SUPPORT_CONTACT for technical support.",
    "Exception",
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Warning
    )
    }
    }
    }