How to force grid to propagate value to datasource immediately on change?

Dmitry picture Dmitry · Mar 22, 2012 · Viewed 10.3k times · Source

I have a DevExpress' XtraGrid which is bound to a collection of objects. I want changes to get into the underlying datasource immediately on change. But the default DevExpress behavior is to put new values into the datasource only when the user has left the cell. So by default when the user types "Hello world" into a cell, the datasource will receive the whole sentence in one go. But I want it to receive "H", "He", "Hel" and so on.

I tried to call PostEditor() in CellValueChanging event handler but it didn't help. Any other ideas?

Answer

DmitryG picture DmitryG · Mar 22, 2012

Grid's in-place editors provide the EditValueChanged event that occur when an end-user types within the editor or changes its value somehow. You can handle this event to post the currently edited value to the data source.
So, I recommend you use the following approach:

    //...
    gridView.ShownEditor += gridView_ShownEditor;
    gridView.HiddenEditor += gridView_HiddenEditor;
}
DevExpress.XtraEditors.BaseEdit gridViewActiveEditor;
void gridView_ShownEditor(object sender, EventArgs e) {
    gridViewActiveEditor = gridView.ActiveEditor;
    gridViewActiveEditor.EditValueChanged += ActiveEditor_EditValueChanged;
}
void gridView_HiddenEditor(object sender, EventArgs e) {
    gridViewActiveEditor.EditValueChanged -= ActiveEditor_EditValueChanged;
}
void ActiveEditor_EditValueChanged(object sender, EventArgs e) {
    gridView.PostEditor();
}