Can I make DataGridView.EndEdit trigger the CellValidating event?

后端 未结 5 583
醉话见心
醉话见心 2021-01-12 02:14

I\'m using a DataGridView in my WinForms application. My main objective is to make the Enter key not move to the next row in the grid. I still want the enter key to validate

相关标签:
5条回答
  • 2021-01-12 02:35

    JJO's code will crash if cell is in edit mode. Below avoids validation exception:

    DataGridViewCell currentCell = AttachedGrid.CurrentCell;
            try
            {             
                AttachedGrid.EndEdit();
                AttachedGrid.CurrentCell = null;
                AttachedGrid.CurrentCell = currentCell;
            }
            catch 
            {
                AttachedGrid.CurrentCell = currentCell;
                AttachedGrid.CurrentCell.Selected = true; 
            }
    

    Source: Kennet Harris's answer here

    0 讨论(0)
  • 2021-01-12 02:37

    thanks for the solution. my version is a slight different from yours, because when i move to the other cell, and my code returns e.cancel=false in the cell validating event, an error will be generated, says that: "operation did not succeed, because the program cannot commit or quit a cell value change". so i put try catch to overcome this problem.

    this is my code:

    Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    
        Dim key As Keys = (keyData And Keys.KeyCode)
    
        If key = Keys.Enter Then
            If MyBase.CurrentCell.ColumnIndex = 1 Then
                Dim iRow As Integer = MyBase.CurrentCell.RowIndex
    
                MyBase.EndEdit()
                Try
                    MyBase.CurrentCell = Nothing
                    MyBase.CurrentCell = MyBase.Rows(iRow).Cells(1)
                    frmFilter.cmdOk_Click(Me, New EventArgs)
                Catch ex As Exception
                End Try
    
                Return True
            End If
        End If
    
        Return MyBase.ProcessDialogKey(keyData)
    End Function
    
    0 讨论(0)
  • 2021-01-12 02:44

    No, but you can manually fire the CellValidating event. Just create the proper parameters. All events are is a class using the Observer Pattern, they're no different than any other method. If that doesn't work, you can create a KeyPress event on the cell and emulate pressing Enter on the cell, but that may mess with the users UI, just put the carat back where it was.

    0 讨论(0)
  • 2021-01-12 02:45

    if your DataGridView's DataSource is BindingSouce, do this (put this in your Key processing events):

    bds.EndEdit();
    

    if your DataGridView's DataSource is DataTable:

    this.BindingContext[dgv.DataSource].EndCurrentEdit();
    
    0 讨论(0)
  • 2021-01-12 02:56

    CellValidating doesn't get called until you change the CurrentCell. So the way I kludged around this was to change the CurrentCell, then switch back to the current one.

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                DataGridViewCell currentCell = CurrentCell;
                EndEdit();
                CurrentCell = null;
                CurrentCell = currentCell;
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }
    
    0 讨论(0)
提交回复
热议问题