Ticket A343
Visible to All Users

How to set a value of another Grid column during editing of a cell

created 21 years ago (modified a year ago)

Description:
Assume a situation where a value of one of two adjacent grid columns depends on another column's value. The first column's value must be changed when a user has edited a value of the second column. How to accomplish this with the ExpressQuantumGrid?

Answer:
Basically, we recommend that you implement such tasks at the dataset level. For example, when a particular field is updated, you may change the other field's value within the field's OnChange event handler or within the dataset's BeforePost event handler.
This, however, applies only to data-aware Views. When working with an unbound View, please use the Data Controller's OnRecordChanged event (it is fired when record data is changed).
You may also try the following approach which can be used for all types of View:
During editing the Data Controller caches edited values and save them to the Values list only after posting. The Data Controller provides two methods to work with edited values: GetEditValue and SetEditValue. You need to use just these methods to work with editing data; the GetValue and SetValue methods are not appropriate for this task.
The Grid's in-place editor provides a number of events, which you can use to handle value changes in the editor. They are OnChange, OnEditValueChanged and OnValidate. We used the OnEditValueChanged event in our sample project and obtained the current edit value via the editor's EditValue property (see the "How to get a new value within the OnEditValueChanged or OnChange event handlers" article).

Delphi
// Delphi code uses cxVariants, cxDataUtils; procedure TfrmMain.viewOrdersAmountPaidPropertiesEditValueChanged( Sender: TObject); var Edit: TcxCustomEdit; Value: Double; View: TcxGridDBTableView; FreightColumn: TcxGridColumn; begin Edit := Sender as TcxCustomEdit; if VarIsNumericEx(Edit.EditValue) then begin Value := Edit.EditValue; Edit.PostEditValue; // To 'stick' the new value in the active cell View := cxGrid.FocusedView as TcxGridDBTableView; FreightColumn := View.GetColumnByFieldName('Freight'); View.DataController.SetEditValue(FreightColumn.Index, Value/5, evsValue); end; end;

NOTE: The SetEditValue method has the benefit that the Grid automatically restores a previous value, if a user has decided to abandon changes and pressed Esc.
See Also:
How to add or delete records in Smart Refresh mode
How to get a new value within the OnEditValueChanged or OnChange event handlers

Show previous comments (6)
DevExpress Support Team 11 years ago

    Hello Bogdan,

    By design this is not done automatically. This is how our grid works. It is necessary to post/save the edited value before editing another cell. Since the DataController.SetEditValue method partially emulates end-user actions, only one cell can be under editing at once.

      Hi Paulo,

      I have tried this solution but dosn't works. I implemented this to my scenario, but the SetEditValue dosn't change the cell, because internal in the  CanInitEditing / CanModify / IsSyncMode / IsDataSetCurrent returns false. My detail dataset is in sql mode (DetailIsInSQLMode = True), can this produce this problem? If yes, can you suggest me an another solution to automatically change a field, if an other was changed by the user? I use FireDAC with parameterized TFDQuerys in master detail scenario.

      This is urgent, please give me a quick workaround!

      Regards,

      Péter

      DevExpress Support Team 9 years ago

        Hello Péter,

        I've created a separate ticket on your behalf (How to change a value of another Grid column during editing of a cell). It has been placed in our processing queue and will be answered shortly.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.