3

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?

Dmitry
  • 1,220
  • 2
  • 11
  • 18

3 Answers3

9

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();
}
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • The HiddenEditor event is called when the editor has been closed, so gridView.ActiveEditor is null. Is there a different event that can be handled to remove the EditValueChanged handler? – Yuyo Jul 24 '14 at 15:50
  • @Yuyo You can anyway to store the gridView.ActiveEditor value into field-variable within the ShownEditor event and then use this value to unsubscribe (I have updated my answer accordingly). – DmitryG Jul 28 '14 at 04:57
2

I think CellValueChanging is the event to trap but instead of PostEditor() try UpdateCurrentRow().

Brad Rem
  • 6,036
  • 2
  • 25
  • 50
  • Thanks! Didn't help though.. The same behavior - value gets into the datasource only when I leave the cell. – Dmitry Mar 22 '12 at 16:35
0

This code in the view's CellValueChanging event handler solved the problem:

    private void OnCellValueChanging(object sender, CellValueChangedEventArgs e)
    {
        _gridView.SetFocusedRowCellValue(_gridView.FocusedColumn, e.Value);
    }
Dmitry
  • 1,220
  • 2
  • 11
  • 18
  • 2
    I should say that it is wrong solution, that can introduce multiple problems (e.g. you've kill the validating of editor, and also lost editor's cursor position) – DmitryG Mar 23 '12 at 20:35
  • 1
    It is not working if the cell editor is text editor, because SetFocusedRowCellValue will always resets cursor position. – Adiono Nov 30 '14 at 02:54