下面的示例演示了如何把不同的内置编辑器指派到列,它们分别被用于显示模式和编辑模式。

假设一个网格列显示整数值,在显示模式中应该使用进度条来呈现数据。 在编辑模式中,单元格的取值必须通过一个微调编辑器来编辑。 要实现此任务,必须通过 GridColumn.ColumnEdit 属性,把一个 ProgressBar 内置编辑器指派作为列的默认编辑器。 此编辑器被用于在显示模式中呈现数据。 为了提供一个用作内置编辑的自定义编辑器,接管了 GridView.CustomRowCellEditForEditing 事件。

下面的插图举例说明了运行结果:

C#CopyCode image复制代码
using DevExpress.XtraEditors.Repository;

// In-place editors used in display and edit modes respectively.
RepositoryItem editorForDisplay, editorForEditing;

private void Form1_Load(object sender, EventArgs e) {
    // Initialize the editors and assign the default editor to a column.
    editorForDisplay = new RepositoryItemProgressBar();
    editorForEditing = new RepositoryItemSpinEdit();
    gridView1.GridControl.RepositoryItems.AddRange( 
      new RepositoryItem[] { editorForDisplay, editorForEditing });
    gridView1.Columns["Quantity"].ColumnEdit = editorForDisplay;
}

// Provide the editor for in-place editing.
private void gridView1_CustomRowCellEditForEditing(object sender, 
  CustomRowCellEditEventArgs e) {
    if (e.Column.FieldName == "Quantity")
        e.RepositoryItem = editorForEditing;
}

Visual BasicCopyCode image复制代码
Imports DevExpress.XtraEditors.Repository

' In-place editors used in display and edit modes respectively.
Dim editorForDisplay, editorForEditing As RepositoryItem

Private Sub Form1_Load(ByVal sender As System.Object, _ 
  ByVal e As System.EventArgs) Handles MyBase.Load
    ' Initialize the editors and assign the default editor to a column.
    editorForDisplay = New RepositoryItemProgressBar()
    editorForEditing = New RepositoryItemSpinEdit()
    gridView1.GridControl.RepositoryItems.AddRange( _
      New RepositoryItem() {editorForDisplay, editorForEditing})
    gridView1.Columns("Quantity").ColumnEdit = editorForDisplay
End Sub

' Provide the editor for in-place editing.
Private Sub gridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, _
  ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) _
  Handles gridView1.CustomRowCellEditForEditing
    If e.Column.FieldName = "Quantity" Then
        e.RepositoryItem = editorForEditing
    End If
End Sub