下面的示例展示了如何防止数字字符 (“0”——“9”) 被输入到网格的 CustomerID 列中。 接管了两个事件来防止输入这些字符:

如果需要在这些事件处理程序中需要丢弃特定的字符,则必须把 Handled 参数设置为 true

C#CopyCode image复制代码
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

// Fires when no in-place editor is active
private void gridView1_KeyPress(object sender, KeyPressEventArgs e) {
    GridView view = sender as GridView;
    string s = "0123456789";
    if (view.FocusedColumn.FieldName == "CustomerID" && s.IndexOf(e.KeyChar) >= 0)
        e.Handled = true;
}
// Fires when an in-place editor is active
private void gridControl1_EditorKeyPress(object sender, KeyPressEventArgs e) {
    GridControl grid = sender as GridControl;
    gridView1_KeyPress(grid.FocusedView, e);
}
Visual BasicCopyCode image复制代码
Imports System.Windows.Forms
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

' Fires when no in-place editor is active.
Private Sub GridView1_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) _
  Handles GridView1.KeyPress
    Dim view As GridView = CType(sender, GridView)
    Dim s As String = "0123456789"
    If view.FocusedColumn.FieldName = "CustomerID" And s.IndexOf(e.KeyChar) >= 0 Then
        e.Handled = True
    End If
End Sub

' Fires when an in-place editor is active.
Private Sub GridControl1_EditorKeyPress(ByVal sender As System.Object, _
  ByVal e As KeyPressEventArgs) _
  Handles GridControl1.EditorKeyPress
    Dim grid As GridControl = CType(sender, GridControl)
    GridView1_KeyPress(grid.FocusedView, e)
End Sub