Back to Devexpress

How to: Set a Cell Value When Another Column Value is Changed

windowsforms-3039-controls-and-libraries-data-grid-examples-data-editing-how-to-set-a-cell-value-when-another-column-value-is-changed.md

latest1.5 KB
Original Source

How to: Set a Cell Value When Another Column Value is Changed

  • Nov 13, 2018

The following sample code handles the ColumnView.CellValueChanged event to update the FullName column value after the FirstName column value has been changed.

csharp
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.BandedGrid;

private void bandedGridView1_CellValueChanged(object sender, CellValueChangedEventArgs e) {
    BandedGridView view = sender as BandedGridView;
    if (view == null) return;
    if (e.Column.Caption != "FirstName") return;
    string cellValue = e.Value.ToString() + " " + view.GetRowCellValue(e.RowHandle, view.Columns["LastName"]).ToString();
    view.SetRowCellValue(e.RowHandle, view.Columns["FullName"], cellValue);
}
vb
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.BandedGrid

Private Sub BandedGridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles BandedGridView1.CellValueChanged
    Dim view As BandedGridView = sender
    If view Is Nothing Then
        Return
    End If
    If e.Column.Caption <> "FirstName" Then
        Return
    End If
    Dim cellValue As String = e.Value.ToString() + " " + view.GetRowCellValue(e.RowHandle, view.Columns("LastName")).ToString()
    view.SetRowCellValue(e.RowHandle, view.Columns("FullName"), cellValue)
End Sub