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
The documentation is incorrect. It says "Occurs after assigning a new value to the TcxCustomEdit.EditValue property." whereas it should say "Occurs BEFORE assigning a new value to the TcxCustomEdit.EditValue property.".
Hello Frederic,
If you mean the "TcxCustomEditProperties.OnEditValueChanged" help topic, it looks correct. This event occurs after the TcxCustomEdit.EditValue is changed. Would you please clarify why you think it does not?
can't understand
Hello,
If something in this Knowledge Base article is unclear to you or you're having any problem with our controls, I suggest that you create a separate ticket and ask your question there. Note that the more details you provide the more efficiently we can help you accomplish your task.
k thanks'
Why is it necessary to call Edit.PostEditValue? I understand what the line comment says but I don't understand why we I need to stick the value. I thought that's done behind the scenes.
I am asking this because indeed, without it, the value is not saved which I found very surprising.
Thanks
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
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.