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
datagridview|only New row editable
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.
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.
Re: datagridview|only New row editable
- #------------------------------------------------------------------------
- #----------------------------------------------
- #region Application Functions
- #----------------------------------------------
- #endregion Application Functions
- #----------------------------------------------
- # Generated Form Function
- #----------------------------------------------
- function Show-tesrt_psf {
- #----------------------------------------------
- #region Import the Assemblies
- #----------------------------------------------
- [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
- [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
- #endregion Import Assemblies
- #----------------------------------------------
- #region Generated Form Objects
- #----------------------------------------------
- [System.Windows.Forms.Application]::EnableVisualStyles()
- $form1 = New-Object 'System.Windows.Forms.Form'
- $datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
- $Column1 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
- $Column2 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
- $Column3 = New-Object 'System.Windows.Forms.DataGridViewButtonColumn'
- $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
- #endregion Generated Form Objects
- #----------------------------------------------
- # User Generated Script
- #----------------------------------------------
- $form1_Load={
- #TODO: Initialize Form Controls here
- }
- #region Control Helper Functions
- function Update-DataGridView
- {
- <#
- .SYNOPSIS
- This functions helps you load items into a DataGridView.
- .DESCRIPTION
- Use this function to dynamically load items into the DataGridView control.
- .PARAMETER DataGridView
- The DataGridView control you want to add items to.
- .PARAMETER Item
- The object or objects you wish to load into the DataGridView's items collection.
- .PARAMETER DataMember
- Sets the name of the list or table in the data source for which the DataGridView is displaying data.
- .PARAMETER AutoSizeColumns
- Resizes DataGridView control's columns after loading the items.
- #>
- Param (
- [ValidateNotNull()]
- [Parameter(Mandatory=$true)]
- [System.Windows.Forms.DataGridView]$DataGridView,
- [ValidateNotNull()]
- [Parameter(Mandatory=$true)]
- $Item,
- [Parameter(Mandatory=$false)]
- [string]$DataMember,
- [System.Windows.Forms.DataGridViewAutoSizeColumnMode]$AutoSizeColumns = 'None'
- )
- $DataGridView.SuspendLayout()
- $DataGridView.DataMember = $DataMember
- if ($Item -is [System.Data.DataSet] -and $Item.Tables.Count -gt 0)
- {
- $DataGridView.DataSource = $Item.Tables[0]
- }
- elseif ($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
- }
- if ($AutoSizeColumns -ne 'None')
- {
- $DataGridView.AutoResizeColumns($AutoSizeColumns)
- }
- $DataGridView.ResumeLayout()
- }
- function ConvertTo-DataTable
- {
- <#
- .SYNOPSIS
- Converts objects into a DataTable.
- .DESCRIPTION
- Converts objects into a DataTable, which are used for DataBinding.
- .PARAMETER InputObject
- The input to convert into a DataTable.
- .PARAMETER Table
- The DataTable you wish to load the input into.
- .PARAMETER RetainColumns
- This switch tells the function to keep the DataTable's existing columns.
- .PARAMETER FilterWMIProperties
- This switch removes WMI properties that start with an underline.
- .EXAMPLE
- $DataTable = ConvertTo-DataTable -InputObject (Get-Process)
- #>
- [OutputType([System.Data.DataTable])]
- param(
- [ValidateNotNull()]
- $InputObject,
- [ValidateNotNull()]
- [System.Data.DataTable]$Table,
- [switch]$RetainColumns,
- [switch]$FilterWMIProperties)
- if($null -eq $Table)
- {
- $Table = New-Object System.Data.DataTable
- }
- if ($InputObject -is [System.Data.DataTable])
- {
- $Table = $InputObject
- }
- elseif ($InputObject -is [System.Data.DataSet] -and $InputObject.Tables.Count -gt 0)
- {
- $Table = $InputObject.Tables[0]
- }
- else
- {
- if (-not $RetainColumns -or $Table.Columns.Count -eq 0)
- {
- #Clear out the Table Contents
- $Table.Clear()
- if ($null -eq $InputObject) { return } #Empty Data
- $object = $null
- #find the first non null value
- foreach ($item in $InputObject)
- {
- if ($null -ne $item)
- {
- $object = $item
- break
- }
- }
- if ($null -eq $object) { return } #All null then empty
- #Get all the properties in order to create the columns
- foreach ($prop in $object.PSObject.Get_Properties())
- {
- if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties
- {
- #Get the type from the Definition string
- $type = $null
- if ($null -ne $prop.Value)
- {
- try { $type = $prop.Value.GetType() }
- catch { Out-Null }
- }
- if ($null -ne $type) # -and [System.Type]::GetTypeCode($type) -ne 'Object')
- {
- [void]$table.Columns.Add($prop.Name, $type)
- }
- else #Type info not found
- {
- [void]$table.Columns.Add($prop.Name)
- }
- }
- }
- if ($object -is [System.Data.DataRow])
- {
- foreach ($item in $InputObject)
- {
- $Table.Rows.Add($item)
- }
- return @( ,$Table)
- }
- }
- else
- {
- $Table.Rows.Clear()
- }
- foreach ($item in $InputObject)
- {
- $row = $table.NewRow()
- if ($item)
- {
- foreach ($prop in $item.PSObject.Get_Properties())
- {
- if ($table.Columns.Contains($prop.Name))
- {
- $row.Item($prop.Name) = $prop.Value
- }
- }
- }
- [void]$table.Rows.Add($row)
- }
- }
- return @(,$Table)
- }
- #endregion
- $datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
- #TODO: Place custom script here
- $datagridview1.EndEdit()
- }
- # --End User Generated Script--
- #----------------------------------------------
- #region Generated Events
- #----------------------------------------------
- $Form_StateCorrection_Load=
- {
- #Correct the initial state of the form to prevent the .Net maximized form issue
- $form1.WindowState = $InitialFormWindowState
- }
- $Form_Cleanup_FormClosed=
- {
- #Remove all event handlers from the controls
- try
- {
- $datagridview1.remove_CellContentClick($datagridview1_CellContentClick)
- $form1.remove_Load($form1_Load)
- $form1.remove_Load($Form_StateCorrection_Load)
- $form1.remove_FormClosed($Form_Cleanup_FormClosed)
- }
- catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
- }
- #endregion Generated Events
- #----------------------------------------------
- #region Generated Form Code
- #----------------------------------------------
- $form1.SuspendLayout()
- #
- # form1
- #
- $form1.Controls.Add($datagridview1)
- $form1.AutoScaleDimensions = '6, 13'
- $form1.AutoScaleMode = 'Font'
- $form1.ClientSize = '588, 344'
- $form1.Name = 'form1'
- $form1.Text = 'Form'
- $form1.add_Load($form1_Load)
- #
- # datagridview1
- #
- $datagridview1.AllowUserToDeleteRows = $False
- $datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
- [void]$datagridview1.Columns.Add($Column1)
- [void]$datagridview1.Columns.Add($Column2)
- [void]$datagridview1.Columns.Add($Column3)
- $datagridview1.Location = '13, 13'
- $datagridview1.Name = 'datagridview1'
- $datagridview1.Size = '503, 298'
- $datagridview1.TabIndex = 0
- $datagridview1.add_CellContentClick($datagridview1_CellContentClick)
- #
- # Column1
- #
- $Column1.HeaderText = 'Column1'
- $Column1.Name = 'Column1'
- $Column1.ReadOnly = $True
- #
- # Column2
- #
- $Column2.HeaderText = 'Column2'
- $Column2.Name = 'Column2'
- $Column2.ReadOnly = $True
- #
- # Column3
- #
- $Column3.HeaderText = 'Column3'
- $Column3.Name = 'Column3'
- $form1.ResumeLayout()
- #endregion Generated 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($Form_StateCorrection_Load)
- #Clean up the control events
- $form1.add_FormClosed($Form_Cleanup_FormClosed)
- #Show the Form
- return $form1.ShowDialog()
- } #End Function
- #Call the form
- 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 .
Re: datagridview|only New row editable
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.
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.
Re: datagridview|only New row editable
Here are some docs on editing: https://docs.microsoft.com/en-us/dotnet ... mework-4.8