Back to Devexpress

ColumnView.ShowingEditor Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-16d0a356.md

latest12.8 KB
Original Source

ColumnView.ShowingEditor Event

Occurs when a cell editor is about to open, and allows you to cancel the action.

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Editor")]
public event CancelEventHandler ShowingEditor
vb
<DXCategory("Editor")>
Public Event ShowingEditor As CancelEventHandler

Event Data

The ShowingEditor event's data class is CancelEventArgs. The following properties provide information specific to this event:

PropertyDescription
CancelGets or sets a value indicating whether the event should be canceled.

Remarks

Handle the ShowingEditor event to prevent a user from editing specific cell values based on a condition. Set the e.Cancel event parameter to true to prevent the editor from being shown. To identify the focused cell, use the view’s FocusedRowHandle and FocusedColumn properties.

To perform specific actions after the editor is shown, handle the ShownEditor event.

Read the following topic for additional information and examples: Disable Cells and Rows Based on a Condition.

Note

The ShowingEditor event does not fire for cells in the Auto Filter Row.

Example – Disable Cells in ‘ID’ Column in Even Rows

The following example suppresses cell editor activation for cells in the “ID” column that belong to even rows:

csharp
gridView.ShowingEditor += (s, e) => {
    e.Cancel = gridView.FocusedColumn.FieldName == "ID" && gridView.FocusedRowHandle % 2 == 0;
};
vb
AddHandler gridView.ShowingEditor, Sub(s, e) e.Cancel = gridView.FocusedColumn.FieldName = "ID" AndAlso gridView.FocusedRowHandle Mod 2 = 0

Run Demo: Prohibit Editing Certain Cells

Example – Disable Cells Based on Values in Other Cells

The following example disables cells in the “Value” column based on values in the “Allow Edit” column:

csharp
using DevExpress.XtraGrid.Views.Grid;
using System.ComponentModel;

namespace DXApplication
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            gridControl1.DataSource = new BindingList<DataItem>() {
                new DataItem(){ Value = "Value 1", AllowEdit = true },
                new DataItem(){ Value = "Value 2", AllowEdit = false },
                new DataItem(){ Value = "Value 3", AllowEdit = false },
                new DataItem(){ Value = "Value 4", AllowEdit = true },
            };
            gridView1.ShowingEditor += GridView1_ShowingEditor;
        }

        void GridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView view = (GridView)sender;
            if (view.FocusedColumn.FieldName == "Value")
                e.Cancel = !(bool)view.GetFocusedRowCellValue("AllowEdit");
        }
    }
    public class DataItem
    {
        public string Value { get; set; }
        public bool AllowEdit { get; set; }
    }
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports System.ComponentModel

Namespace DXApplication
    Partial Public Class Form1
        Inherits DevExpress.XtraEditors.XtraForm

        Public Sub New()
            InitializeComponent()
            gridControl1.DataSource = New BindingList(Of DataItem)() From {
                New DataItem() With {
                    .Value = "Value 1",
                    .AllowEdit = True
                },
                New DataItem() With {
                    .Value = "Value 2",
                    .AllowEdit = False
                },
                New DataItem() With {
                    .Value = "Value 3",
                    .AllowEdit = False
                },
                New DataItem() With {
                    .Value = "Value 4",
                    .AllowEdit = True
                }
            }
            AddHandler gridView1.ShowingEditor, AddressOf GridView1_ShowingEditor
        End Sub

        Private Sub GridView1_ShowingEditor(ByVal sender As Object, ByVal e As CancelEventArgs)
            Dim view As GridView = DirectCast(sender, GridView)
            If view.FocusedColumn.FieldName = "Value" Then
                e.Cancel = Not CBool(view.GetFocusedRowCellValue("AllowEdit"))
            End If
        End Sub
    End Class
    Public Class DataItem
        Public Property Value() As String
        Public Property AllowEdit() As Boolean
    End Class
End Namespace

Example – Make Specific Cells Read Only Based on Values in Other Cells

The following example handles the CustomRowCellEdit event to assign the readonlyTextEdit to cells in the “Value” column based on values in the “Allow Edit” column:

csharp
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using System.ComponentModel;

namespace DXApplication6
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        RepositoryItemTextEdit readonlyTextEdit;
        public Form1()
        {
            InitializeComponent();
            gridControl1.DataSource = new BindingList<DataItem>() {
                new DataItem(){ Value = "Value 1", AllowEdit = true },
                new DataItem(){ Value = "Value 2", AllowEdit = false },
                new DataItem(){ Value = "Value 3", AllowEdit = false },
                new DataItem(){ Value = "Value 4", AllowEdit = true },
            };

            readonlyTextEdit = new RepositoryItemTextEdit() { ReadOnly = true };
            gridControl1.RepositoryItems.Add(readonlyTextEdit);

            gridView1.CustomRowCellEdit += GridView1_CustomRowCellEdit;
        }
        void GridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
        {
            GridView view = (GridView)sender;
            if (e.Column.FieldName == "Value")
                if (!(bool)view.GetRowCellValue(e.RowHandle, "AllowEdit"))
                    e.RepositoryItem = readonlyTextEdit;
        }
    }
    public class DataItem
    {
        public string Value { get; set; }
        public bool AllowEdit { get; set; }
    }
}
vb
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid.Views.Grid
Imports System.ComponentModel

Namespace DXApplication6
    Partial Public Class Form1
        Inherits DevExpress.XtraEditors.XtraForm

        Private readonlyTextEdit As RepositoryItemTextEdit
        Public Sub New()
            InitializeComponent()
            gridControl1.DataSource = New BindingList(Of DataItem)() From {
                New DataItem() With {
                    .Value = "Value 1",
                    .AllowEdit = True
                },
                New DataItem() With {
                    .Value = "Value 2",
                    .AllowEdit = False
                },
                New DataItem() With {
                    .Value = "Value 3",
                    .AllowEdit = False
                },
                New DataItem() With {
                    .Value = "Value 4",
                    .AllowEdit = True
                }
            }

            readonlyTextEdit = New RepositoryItemTextEdit() With {.ReadOnly = True}
            gridControl1.RepositoryItems.Add(readonlyTextEdit)

            AddHandler gridView1.CustomRowCellEdit, AddressOf GridView1_CustomRowCellEdit
        End Sub
        Private Sub GridView1_CustomRowCellEdit(ByVal sender As Object, ByVal e As CustomRowCellEditEventArgs)
            Dim view As GridView = DirectCast(sender, GridView)
            If e.Column.FieldName = "Value" Then
                If Not CBool(view.GetRowCellValue(e.RowHandle, "AllowEdit")) Then
                    e.RepositoryItem = readonlyTextEdit
                End If
            End If
        End Sub
    End Class
    Public Class DataItem
        Public Property Value() As String
        Public Property AllowEdit() As Boolean
    End Class
End Namespace

Tip

To disable all cells in the View, use the View’s OptionsBehavior.Editable property.

Turn off the column’s OptionsColumn.AllowEdit option to disable its cells.

Online Video – Prevent Editing in Specific Cells

Watch Video: Prevent Editing in Specific Cells

The following code snippets (auto-collected from DevExpress Examples) contain references to the ShowingEditor event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-grid-display-custom-rows/CS/CustomRowInGrid/CustomRowInGrid/SummaryItem.cs#L24

csharp
View = view;
View.ShowingEditor += new CancelEventHandler(View_ShowingEditor);

winforms-grid-copy-cell-value-to-other-cells-by-dragging-its-right-bottom-edge/CS/Classes/DragCellsValuesHelper.cs#L44

csharp
View.MouseUp += new MouseEventHandler(View_MouseUp);
View.ShowingEditor += new CancelEventHandler(View_ShowingEditor);
View.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(View_SelectionChanged);

winforms-grid-display-custom-rows/VB/CustomRowInGrid/SummaryItem.vb#L21

vb
Me.View = view
AddHandler Me.View.ShowingEditor, AddressOf View_ShowingEditor

winforms-grid-copy-cell-value-to-other-cells-by-dragging-its-right-bottom-edge/VB/Classes/DragCellsValuesHelper.vb#L44

vb
AddHandler View.MouseUp, AddressOf View_MouseUp
AddHandler View.ShowingEditor, AddressOf View_ShowingEditor
AddHandler View.SelectionChanged, AddressOf View_SelectionChanged

See Also

ShownEditor

HiddenEditor

Edit Data. Create Cell Editors. Validate User Input

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace