Onvalidating in editorcontrol

styxke

Member
Joined
May 18, 2006
Messages
16
Programming Experience
5-10
Hi,

Is it possible to use an Onvalidating event in an editing control in a datagridview?
I wrote something like this:
VB.NET:
Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
    MyBase.OnValidating(e)
    If Me.Text <> String.Empty Then
       If Me.Text <> "CORRECT"
          e.Cancel = True
       End If
    End If
  End Sub

But when tabbing out the cell it gets executed twice plus that the incorrect value in the cell remains and the I can continue to edit in other cells.The event is correctly handled when I click somewhere else with the mouse (being the cursor remaining in the cell until a correct value is given).

Do I need to do something special when the tab key is pressed.

Any help is appreciated.

Thanks in advance.
 
Last edited:
I assume that you have provided more functionality than that because you'd normally just handle the CellValidating event of the grid to do something like that. I think that you'd propbably need to perform your custom processing before calling the base implementation.
 
Thanks for your answer.

I override these methods in the editing control:
VB.NET:
[SIZE=2]OnKeyPress,[/SIZE][SIZE=2]OnTextChanged,[/SIZE][SIZE=2]OnGotFocus,[/SIZE][SIZE=2]OnValidating[/SIZE]
VB.NET:
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
  MyBase.OnKeyPress(e)
  If Me.Uppercase Then
    e.KeyChar = Char.ToUpper(e.KeyChar)
  End If
  Me.valueIsChanged = True
  Me.dataGridView.NotifyCurrentCellDirty(True)
End Sub
 
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
  MyBase.OnTextChanged(e)
  If Me.Focused Then
    Me.valueIsChanged = True
    Me.dataGridView.NotifyCurrentCellDirty(True)
  End If
End Sub
 
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
  MyBase.OnGotFocus(e)
  If Me.Parent.FindForm.GetType.BaseType Is GetType(BaseDialog) Or Me.Parent.FindForm.GetType.BaseType Is GetType(BaseForm) Then
    If _helpfield Then
      Dim _help As Control = SearchHelpButton(Me.Parent.FindForm.Controls)
      If Not IsNothing(_help) Then
        CType(_help, CommonButton).Enabled = True
      End If
    Else
      Dim _help As Control = SearchHelpButton(Me.Parent.FindForm.Controls)
      If Not IsNothing(_help) Then
        CType(_help, CommonButton).Enabled = False
      End If
    End If
  End If
End Sub
 
Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
  MyBase.OnValidating(e)
  If Me.Text <> String.Empty Then
    If Me._type = Mode.Article Then
      Dim _objArtikel As INTERN = New INTERN(New UserContext, ApplicationSettings.AppCon.Environment)
      _objArtikel.GetINTERNByArtno(Me.Text)
      If Not _objArtikel.IsDbOrigin Then
        MsgBox("Article doesn't exist", MsgBoxStyle.Critical)
        e.Cancel = True
      End If
    End If
  End If
End Sub
The code in OnGotFocus searches for a a button of the type help if the column is of type helpfield and enables it.
The rest is all pretty standard (in the editing control, cell and column).

I'll try to explain the situation:
I need a custom column which is reusable throughout my projects. The column has a property ColumnType (eg. Article, Order, ...) and when the users inputs something in the cell it's validates the input taking into account the type of the column, so when it's of type Article it checks if the article exists in the database for example.

I don't want to rewrite the CellValidating code on every datagrid when I need this type of column ... I want to handle it within the custom column so that I only need to write it once and can reuse it indefinately. So I wrote it in the editing control that is used by the custom datagridviewcell which is used in the custom datagridviewcolumn.

I tried performing the custom processing before the base processing, it had no effect.
 
Last edited:
Back
Top