datagridview|only New row editable

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 4 years and 9 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
jcf53100
Posts: 3
Last visit: Wed Jun 12, 2019 11:31 am

datagridview|only New row editable

Post by jcf53100 »

Hello ,

I have datagridview who working fine .The data can't be edited because I locked this option .

I would like to enable editing for the last row only and then i will update the datagridview ,how can i dot it ?

Powershell V5
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview|only New row editable

Post by jvierra »

Use "BeginEdit()" and "EndEdit()"
jcf53100
Posts: 3
Last visit: Wed Jun 12, 2019 11:31 am

Re: datagridview|only New row editable

Post by jcf53100 »

  1. #------------------------------------------------------------------------
  2. #----------------------------------------------
  3. #region Application Functions
  4. #----------------------------------------------
  5.  
  6. #endregion Application Functions
  7.  
  8. #----------------------------------------------
  9. # Generated Form Function
  10. #----------------------------------------------
  11. function Show-tesrt_psf {
  12.  
  13.     #----------------------------------------------
  14.     #region Import the Assemblies
  15.     #----------------------------------------------
  16.     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  17.     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  18.     #endregion Import Assemblies
  19.  
  20.     #----------------------------------------------
  21.     #region Generated Form Objects
  22.     #----------------------------------------------
  23.     [System.Windows.Forms.Application]::EnableVisualStyles()
  24.     $form1 = New-Object 'System.Windows.Forms.Form'
  25.     $datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
  26.     $Column1 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
  27.     $Column2 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
  28.     $Column3 = New-Object 'System.Windows.Forms.DataGridViewButtonColumn'
  29.     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  30.     #endregion Generated Form Objects
  31.  
  32.     #----------------------------------------------
  33.     # User Generated Script
  34.     #----------------------------------------------
  35.    
  36.     $form1_Load={
  37.         #TODO: Initialize Form Controls here
  38.        
  39.     }
  40.    
  41.     #region Control Helper Functions
  42.     function Update-DataGridView
  43.     {
  44.         <#
  45.         .SYNOPSIS
  46.             This functions helps you load items into a DataGridView.
  47.    
  48.         .DESCRIPTION
  49.             Use this function to dynamically load items into the DataGridView control.
  50.    
  51.         .PARAMETER  DataGridView
  52.             The DataGridView control you want to add items to.
  53.    
  54.         .PARAMETER  Item
  55.             The object or objects you wish to load into the DataGridView's items collection.
  56.        
  57.         .PARAMETER  DataMember
  58.             Sets the name of the list or table in the data source for which the DataGridView is displaying data.
  59.    
  60.         .PARAMETER AutoSizeColumns
  61.             Resizes DataGridView control's columns after loading the items.
  62.         #>
  63.         Param (
  64.             [ValidateNotNull()]
  65.             [Parameter(Mandatory=$true)]
  66.             [System.Windows.Forms.DataGridView]$DataGridView,
  67.             [ValidateNotNull()]
  68.             [Parameter(Mandatory=$true)]
  69.             $Item,
  70.             [Parameter(Mandatory=$false)]
  71.             [string]$DataMember,
  72.             [System.Windows.Forms.DataGridViewAutoSizeColumnMode]$AutoSizeColumns = 'None'
  73.         )
  74.         $DataGridView.SuspendLayout()
  75.         $DataGridView.DataMember = $DataMember
  76.        
  77.         if ($Item -is [System.Data.DataSet] -and $Item.Tables.Count -gt 0)
  78.         {
  79.             $DataGridView.DataSource = $Item.Tables[0]
  80.         }
  81.         elseif ($Item -is [System.ComponentModel.IListSource]`
  82.         -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
  83.         {
  84.             $DataGridView.DataSource = $Item
  85.         }
  86.         else
  87.         {
  88.             $array = New-Object System.Collections.ArrayList
  89.            
  90.             if ($Item -is [System.Collections.IList])
  91.             {
  92.                 $array.AddRange($Item)
  93.             }
  94.             else
  95.             {
  96.                 $array.Add($Item)
  97.             }
  98.             $DataGridView.DataSource = $array
  99.         }
  100.        
  101.         if ($AutoSizeColumns -ne 'None')
  102.         {
  103.             $DataGridView.AutoResizeColumns($AutoSizeColumns)
  104.         }
  105.        
  106.         $DataGridView.ResumeLayout()
  107.     }
  108.    
  109.     function ConvertTo-DataTable
  110.     {
  111.         <#
  112.             .SYNOPSIS
  113.                 Converts objects into a DataTable.
  114.        
  115.             .DESCRIPTION
  116.                 Converts objects into a DataTable, which are used for DataBinding.
  117.        
  118.             .PARAMETER  InputObject
  119.                 The input to convert into a DataTable.
  120.        
  121.             .PARAMETER  Table
  122.                 The DataTable you wish to load the input into.
  123.        
  124.             .PARAMETER RetainColumns
  125.                 This switch tells the function to keep the DataTable's existing columns.
  126.            
  127.             .PARAMETER FilterWMIProperties
  128.                 This switch removes WMI properties that start with an underline.
  129.        
  130.             .EXAMPLE
  131.                 $DataTable = ConvertTo-DataTable -InputObject (Get-Process)
  132.         #>
  133.         [OutputType([System.Data.DataTable])]
  134.         param(
  135.         [ValidateNotNull()]
  136.         $InputObject,
  137.         [ValidateNotNull()]
  138.         [System.Data.DataTable]$Table,
  139.         [switch]$RetainColumns,
  140.         [switch]$FilterWMIProperties)
  141.        
  142.         if($null -eq $Table)
  143.         {
  144.             $Table = New-Object System.Data.DataTable
  145.         }
  146.        
  147.         if ($InputObject -is [System.Data.DataTable])
  148.         {
  149.             $Table = $InputObject
  150.         }
  151.         elseif ($InputObject -is [System.Data.DataSet] -and $InputObject.Tables.Count -gt 0)
  152.         {
  153.             $Table = $InputObject.Tables[0]
  154.         }
  155.         else
  156.         {
  157.             if (-not $RetainColumns -or $Table.Columns.Count -eq 0)
  158.             {
  159.                 #Clear out the Table Contents
  160.                 $Table.Clear()
  161.                
  162.                 if ($null -eq $InputObject) { return } #Empty Data
  163.                
  164.                 $object = $null
  165.                 #find the first non null value
  166.                 foreach ($item in $InputObject)
  167.                 {
  168.                     if ($null -ne $item)
  169.                     {
  170.                         $object = $item
  171.                         break
  172.                     }
  173.                 }
  174.                
  175.                 if ($null -eq $object) { return } #All null then empty
  176.                
  177.                 #Get all the properties in order to create the columns
  178.                 foreach ($prop in $object.PSObject.Get_Properties())
  179.                 {
  180.                     if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties
  181.                     {
  182.                         #Get the type from the Definition string
  183.                         $type = $null
  184.                        
  185.                         if ($null -ne $prop.Value)
  186.                         {
  187.                             try { $type = $prop.Value.GetType() }
  188.                             catch { Out-Null }
  189.                         }
  190.                        
  191.                         if ($null -ne $type) # -and [System.Type]::GetTypeCode($type) -ne 'Object')
  192.                         {
  193.                             [void]$table.Columns.Add($prop.Name, $type)
  194.                         }
  195.                         else #Type info not found
  196.                         {
  197.                             [void]$table.Columns.Add($prop.Name)
  198.                         }
  199.                     }
  200.                 }
  201.                
  202.                 if ($object -is [System.Data.DataRow])
  203.                 {
  204.                     foreach ($item in $InputObject)
  205.                     {
  206.                         $Table.Rows.Add($item)
  207.                     }
  208.                     return @( ,$Table)
  209.                 }
  210.             }
  211.             else
  212.             {
  213.                 $Table.Rows.Clear()
  214.             }
  215.            
  216.             foreach ($item in $InputObject)
  217.             {
  218.                 $row = $table.NewRow()
  219.                
  220.                 if ($item)
  221.                 {
  222.                     foreach ($prop in $item.PSObject.Get_Properties())
  223.                     {
  224.                         if ($table.Columns.Contains($prop.Name))
  225.                         {
  226.                             $row.Item($prop.Name) = $prop.Value
  227.                         }
  228.                     }
  229.                 }
  230.                 [void]$table.Rows.Add($row)
  231.             }
  232.         }
  233.        
  234.         return @(,$Table)  
  235.     }
  236.     #endregion
  237.    
  238.     $datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
  239.     #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  240.         #TODO: Place custom script here
  241.         $datagridview1.EndEdit()
  242.     }
  243.    
  244.     # --End User Generated Script--
  245.     #----------------------------------------------
  246.     #region Generated Events
  247.     #----------------------------------------------
  248.    
  249.     $Form_StateCorrection_Load=
  250.     {
  251.         #Correct the initial state of the form to prevent the .Net maximized form issue
  252.         $form1.WindowState = $InitialFormWindowState
  253.     }
  254.    
  255.     $Form_Cleanup_FormClosed=
  256.     {
  257.         #Remove all event handlers from the controls
  258.         try
  259.         {
  260.             $datagridview1.remove_CellContentClick($datagridview1_CellContentClick)
  261.             $form1.remove_Load($form1_Load)
  262.             $form1.remove_Load($Form_StateCorrection_Load)
  263.             $form1.remove_FormClosed($Form_Cleanup_FormClosed)
  264.         }
  265.         catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
  266.     }
  267.     #endregion Generated Events
  268.  
  269.     #----------------------------------------------
  270.     #region Generated Form Code
  271.     #----------------------------------------------
  272.     $form1.SuspendLayout()
  273.     #
  274.     # form1
  275.     #
  276.     $form1.Controls.Add($datagridview1)
  277.     $form1.AutoScaleDimensions = '6, 13'
  278.     $form1.AutoScaleMode = 'Font'
  279.     $form1.ClientSize = '588, 344'
  280.     $form1.Name = 'form1'
  281.     $form1.Text = 'Form'
  282.     $form1.add_Load($form1_Load)
  283.     #
  284.     # datagridview1
  285.     #
  286.     $datagridview1.AllowUserToDeleteRows = $False
  287.     $datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
  288.     [void]$datagridview1.Columns.Add($Column1)
  289.     [void]$datagridview1.Columns.Add($Column2)
  290.     [void]$datagridview1.Columns.Add($Column3)
  291.     $datagridview1.Location = '13, 13'
  292.     $datagridview1.Name = 'datagridview1'
  293.     $datagridview1.Size = '503, 298'
  294.     $datagridview1.TabIndex = 0
  295.     $datagridview1.add_CellContentClick($datagridview1_CellContentClick)
  296.     #
  297.     # Column1
  298.     #
  299.     $Column1.HeaderText = 'Column1'
  300.     $Column1.Name = 'Column1'
  301.     $Column1.ReadOnly = $True
  302.     #
  303.     # Column2
  304.     #
  305.     $Column2.HeaderText = 'Column2'
  306.     $Column2.Name = 'Column2'
  307.     $Column2.ReadOnly = $True
  308.     #
  309.     # Column3
  310.     #
  311.     $Column3.HeaderText = 'Column3'
  312.     $Column3.Name = 'Column3'
  313.     $form1.ResumeLayout()
  314.     #endregion Generated Form Code
  315.  
  316.     #----------------------------------------------
  317.  
  318.     #Save the initial state of the form
  319.     $InitialFormWindowState = $form1.WindowState
  320.     #Init the OnLoad event to correct the initial state of the form
  321.     $form1.add_Load($Form_StateCorrection_Load)
  322.     #Clean up the control events
  323.     $form1.add_FormClosed($Form_Cleanup_FormClosed)
  324.     #Show the Form
  325.     return $form1.ShowDialog()
  326.  
  327. } #End Function
  328.  
  329. #Call the form
  330. Show-tesrt_psf | Out-Null

I provided an example so where can i put BeginEdit() and endEdit() ?

I tried in many place of the script but it's not working .
Thank you for your help .
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview|only New row editable

Post by jvierra »

You have to use an event to add a new row to the grid then set the row to BeginEdit. After that the row is editable.

Search for examples of adding and editing rows in a DGV. I don't have any examples that are available to post as most are in C# and the one in PS is way to complicated for use as a sample.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview|only New row editable

Post by jvierra »

This topic is 4 years and 9 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