When I use EditMode = EditOnEnter, The cell enter on editmode when I select the row.
It is hard to user to select the RowSelector 开发者_如何学Pythonto Delete the row.
do you know any Trick ?
I Try this Trick:
If the user clicks on a row header, change the EditMode to EditOnKeystrokeOrF2
and end the edit.
If the user clicks somewhere else, change the EditMode to EditOnEnter
and begin the edit.
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
You should understand that your program can't really read user's thought and understand when enter edit mode but when don't this.
User can press Escape key to cancel edit mode, then row can be deleted. Also you may choose other DataGridViewEditMode (see http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridvieweditmode.aspx ), for instance DataGridViewEditMode.EditOnKeystroke, or EditOnKeystrokeOrF2 that both are good from the usability point of view.
This really needs to be done on MouseDown. Cell Click events fire on MouseUp:
Private Sub ProductsGrid_MouseDown(sender As Object, e As MouseEventArgs) Handles ProductsGrid.MouseDown
Dim grid = DirectCast(sender, DataGridView)
Dim info = grid.HitTest(e.X, e.Y)
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
There is more that needs to be done as well, including handling Tab Key behaviour. Such behaviour (and the above modification) are probably best done in a user control which inherits DataGridView.
You can change the focus to the parent control. That will force the cell to end editing without having to change the mode itself.
For example this snippet will leave the edit mode if you select more then one cell.
protected override void OnSelectionChanged(EventArgs e)
{
base.OnSelectionChanged(e);
if (SelectedCells.Count > 1)
{
// leave edit mode
Parent?.Focus();
}
}
精彩评论