Datagrid does not allow delete row when "EditOnEnter"

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.
Locked
User avatar
JaimeBou
Posts: 6
Joined: Fri Feb 09, 2018 9:57 am

Datagrid does not allow delete row when "EditOnEnter"

Post by JaimeBou » Sun Dec 16, 2018 11:38 am

I am new to building GUI's in Powershell.

I was able to build out my Datagrid but I am running into an issue where I can't delete rows if the editmode is set to EditOnEnter.

When it's set to EditOnEnter, the focus goes to the cell (appearing as if you are going to enter or change data) and prevents me from deleting the row.

However, If I set the editmode to the other options, as in EditOnKeystroke, I am able to delete the row. Is there a best practice approach to solving this? I prefer the editOnEnter mode; it's easier for the user.

Thank you for your help in advance.

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagrid does not allow delete row when "EditOnEnter"

Post by jvierra » Sun Dec 16, 2018 11:51 am

In your delete code cancel the edit on the row before deleting it.

You can also select the row with the row header which will not start an edit. Once you are in edit mode delete is not available. Use the row selector (header) to select then delete the row.

User avatar
jvierra
Posts: 13392
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagrid does not allow delete row when "EditOnEnter"

Post by jvierra » Sun Dec 16, 2018 1:35 pm

I just tested. Set grid to "EnterOnKeyStroke" to get it to allow deletions. "EditOnEnter" always forces the first cell to be entered whenever the record is selected.

To delete programmatically do the following:

Code: Select all

$buttonDeleteRow_Click = {
    $ix = $datagridview1.SelectedCells[0].RowIndex
    $datagridview1.Rows.Remove($datagridview1.Rows[$ix])
}
This can be done in a button or in a context menu.

User avatar
JaimeBou
Posts: 6
Joined: Fri Feb 09, 2018 9:57 am

Re: Datagrid does not allow delete row when "EditOnEnter"

Post by JaimeBou » Mon Dec 17, 2018 7:23 pm

Thank very much for your help. I will give this a try.

Locked