DataGridView.Editmode = EditOnEnter. How to choose a row to delete?

When I use EditMode = EditOnEnter, the cell enters into editmode when I select a row.

It is difficult for the user to select a RowSelector to delete a row.

Do you know the trick?

+4
source share
4 answers

I try this trick:

When Cell Click:

  • if index = -1 EndEdit and EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
  • Else If EditOnKeystrokeOrF2 - Repair EditMode and BeginEdit

Private Sub dgv2_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv2.CellClick If e.ColumnIndex = -1 Then dgv2.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2 dgv2.EndEdit() ElseIf dgv2.EditMode <> DataGridViewEditMode.EditOnEnter Then dgv2.EditMode = DataGridViewEditMode.EditOnEnter dgv2.BeginEdit(False) End If End Sub 
+7
source

You must understand that your program cannot really read the user's mind and understand when you enter edit mode, but when you do not.

The user can press the Escape key to cancel the editing mode, after which the line can be deleted. You can also select another DataGridViewEditMode (see http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridvieweditmm.aspx ), for example, DataGridViewEditMode.EditOnKeystroke or EditOnKeystrokeOrF2, which both have good views .

+1
source

This really needs to be done on MouseDown. Mouse click cells on MouseUp:

 Private Sub ProductsGrid_MouseDown(sender As Object, e As MouseEventArgs) Handles ProductsGrid.MouseDown Dim grid = DirectCast(sender, DataGridView) Dim info = grid.HitTest(eX, eY) If into.Type = DataGridViewHitTestType.RowHeader OrElse info.Type = DataGridViewHitTestType.TopLeftHeader Then grid.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2 grid.EndEdit() ElseIf grid.EditMode <> DataGridViewEditMode.EditOnEnter Then grid.EditMode = DataGridViewEditMode.EditOnEnter End If End Sub 

Much more needs to be done, including handling Tab Key behavior. This behavior (and the aforementioned modification) is probably best performed in a user control that inherits a DataGridView.

0
source

You can change the focus to the parent control. This will force the cell to finish editing without having to change the mode itself.

For example, this snippet will leave editing mode if you select more than one cell.

 protected override void OnSelectionChanged(EventArgs e) { base.OnSelectionChanged(e); if (SelectedCells.Count > 1) { // leave edit mode Parent?.Focus(); } } 
0
source

Source: https://habr.com/ru/post/1315125/


All Articles