Back to Devexpress

ColumnView.CellValueChanging Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-5408a216.md

latest8.1 KB
Original Source

ColumnView.CellValueChanging Event

Fires when a user changes a cell value: types or deletes a character, chooses a value from the dropdown list, etc. Does not fire when you change cell values in code.

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

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

Declaration

csharp
[DXCategory("Property Changed")]
public event CellValueChangedEventHandler CellValueChanging
vb
<DXCategory("Property Changed")>
Public Event CellValueChanging As CellValueChangedEventHandler

Event Data

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

PropertyDescription
ColumnGets the column that contains the processed cell.
OldValueGets the cell’s previous value.
RowHandleGets the handle of the row that contains the processed cell.
ValueGets the current cell value.

Remarks

The following code sample handles the CellValueChanging event to highlight edited cells.

csharp
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;

public partial class Form1 : Form {
    List<GridChangedCellInfo> changedCells;
    public Form1() {
        InitializeComponent();
        changedCells = new List<GridChangedCellInfo>();
        gridControl1.DataSource = new List<Person> {
            new Person() { ID=1, Name="John Doe", Text="Comments Line 1" },
            new Person() { ID=2, Name="Jill Jones", Text="Comments Line 2" },
            new Person() { ID=3, Name="Cyril James", Text="Comments Line 3" },
        };
        gridView1.CellValueChanging += gridView1_CellValueChanging;
        gridView1.CustomDrawCell += GridView1_CustomDrawCell;
    }
    void gridView1_CellValueChanging(object sender, CellValueChangedEventArgs e) {
        var view = sender as GridView;
        var listSourceRowIndex = view.GetDataSourceRowIndex(e.RowHandle);
        var changedCell = new GridChangedCellInfo(listSourceRowIndex, e.Column);
        if (!changedCells.Contains(changedCell))
            changedCells.Add(changedCell);
    }
    void GridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
        var view = sender as GridView;
        var listSourceRowIndex = view.GetDataSourceRowIndex(e.RowHandle);
        var changedCell = new GridChangedCellInfo(listSourceRowIndex, e.Column);

        if (!changedCells.Contains(changedCell))
            return;
        e.Appearance.BackColor = Color.Red;
        e.Appearance.DrawBackground(e.Cache, e.Bounds);
        e.Appearance.DrawString(e.Cache, e.DisplayText, e.Bounds);
        e.Handled = true;
    }
}

public class GridChangedCellInfo {
    public int ListSourceRowIndex { get; set; }
    public GridColumn Column { get; set; }
    public GridChangedCellInfo(int listSourceRowIndex, GridColumn column) {
        ListSourceRowIndex = listSourceRowIndex;
        Column = column;
    }

    public override bool Equals(object obj) {
        if (!(obj is GridChangedCellInfo))
            return false;

        var gridChangedCellInfo = obj as GridChangedCellInfo;

        return Column.Equals(gridChangedCellInfo.Column)
            && ListSourceRowIndex.Equals(gridChangedCellInfo.ListSourceRowIndex);
    }
    public override int GetHashCode() {
        return base.GetHashCode();
    }
}
public class Person {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns

Partial Public Class Form1
    Inherits Form

    Private changedCells As List(Of GridChangedCellInfo)
    Public Sub New()
        InitializeComponent()
        changedCells = New List(Of GridChangedCellInfo)()
        gridControl1.DataSource = New List(Of Person) From {
            New Person() With {.ID=1, .Name="John Doe", .Text="Comments Line 1"},
            New Person() With {.ID=2, .Name="Jill Jones", .Text="Comments Line 2"},
            New Person() With {.ID=3, .Name="Cyril James", .Text="Comments Line 3"}
        }
        AddHandler gridView1.CellValueChanging, AddressOf gridView1_CellValueChanging
        AddHandler gridView1.CustomDrawCell, AddressOf GridView1_CustomDrawCell
    End Sub
    Private Sub gridView1_CellValueChanging(ByVal sender As Object, ByVal e As CellValueChangedEventArgs)
        Dim view = TryCast(sender, GridView)
        Dim listSourceRowIndex = view.GetDataSourceRowIndex(e.RowHandle)
        Dim changedCell = New GridChangedCellInfo(listSourceRowIndex, e.Column)
        If Not changedCells.Contains(changedCell) Then
            changedCells.Add(changedCell)
        End If
    End Sub
    Private Sub GridView1_CustomDrawCell(ByVal sender As Object, ByVal e As RowCellCustomDrawEventArgs)
        Dim view = TryCast(sender, GridView)
        Dim listSourceRowIndex = view.GetDataSourceRowIndex(e.RowHandle)
        Dim changedCell = New GridChangedCellInfo(listSourceRowIndex, e.Column)

        If Not changedCells.Contains(changedCell) Then
            Return
        End If
        e.Appearance.BackColor = Color.Red
        e.Appearance.DrawBackground(e.Cache, e.Bounds)
        e.Appearance.DrawString(e.Cache, e.DisplayText, e.Bounds)
        e.Handled = True
    End Sub
End Class
Public Class GridChangedCellInfo
    Public Property ListSourceRowIndex() As Integer
    Public Property Column() As GridColumn
    Public Sub New(ByVal listSourceRowIndex As Integer, ByVal column As GridColumn)
        Me.ListSourceRowIndex = listSourceRowIndex
        Me.Column = column
    End Sub

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If Not(TypeOf obj Is GridChangedCellInfo) Then
            Return False
        End If

        Dim gridChangedCellInfo = TryCast(obj, GridChangedCellInfo)

        Return Column.Equals(gridChangedCellInfo.Column) AndAlso ListSourceRowIndex.Equals(gridChangedCellInfo.ListSourceRowIndex)
    End Function
    Public Overrides Function GetHashCode() As Integer
        Return MyBase.GetHashCode()
    End Function
End Class
Public Class Person
    Public Property ID() As Integer
    Public Property Name() As String
    Public Property Text() As String
End Class

Tip

Use the e.RowHandle event parameter to identify whether the user is editing a cell in the data row or the new item row:

csharp
void gridView_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) {
    if (e.RowHandle == DevExpress.XtraGrid.GridControl.NewItemRowHandle) {
        // Handle value changes for the new row.
    }
}

See Also

CellValueChanged

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace