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.
This topic is 5 years and 3 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
User avatar
JaimeBou
Posts: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

Datagrid does not allow delete row when "EditOnEnter"

Post by JaimeBou »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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: 27
Last visit: Wed Jan 31, 2024 8:52 am
Has voted: 1 time

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

Post by JaimeBou »

Thank very much for your help. I will give this a try.
This topic is 5 years and 3 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