Custom EditingControl doesn't set the Value

0
0

Hello,

kind of a continuation of this thread from me: https://wisej.com/support/question/for-some-reason-there-is-sometimes-no-value-in-the-datagridviewcell-in-cellendedit

Back then I solved it by simply extending DataGridViewTextBoxEditingControl, but now I have to extend a different control, so I build a small example, but it doesn’t work.

I looked through these threads: https://wisej.com/support/question/custom-cell-editor-in-datagridview and https://wisej.com/support/question/how-do-i-add-a-custom-control-to-a-data-grid-view and copied parts of the solutions. There seems to be no code where the Textbox manually sets the Value of the Cell.
One Example even says “// Not needed, the DataGridView will store the Text property in the cell’s value.”

But it doesn’t work for me.

During CellEndEdit Eventhandlers, the Value is the old one. CellValueChanged is never hit. Visually the new one is shown, until I start editing it again.

I’m now using 3.0.10. Do Custom Editors need to set Cell Values manually? Why does no example show that this is needed? The interface and the examples of Windows Forms EditingControls seem to work quite differently as well.

I added some sample code. Most things in one file. A standard page that should work on it’s own in a wisej project, when loaded with Application.MainPage = new DataGridEditorPage();

Thanks in advance.

  • You must to post comments
0
0

It’s a lot easier than the code you attached. There are two ways to use custom editors:

Light way:  column1.Editor = new TextBox()

This is all you need. TextBox can be MyTextBox() or whatever control you like. When a cell in column1 enters edit mode the single instance (for that column) of the edit control is moved to that cell, initialized with the cell formatted value and then the Text property of the editor is parsed and stored in the cell.

More involved way: Custom column, custom cell, and custom editor implementing IDataGridViewEditingControl. Of course there are also ways in between where you can change a cell type in a standard column, or change only a single cell, etc.

// custom column using a custom cell
public class RICStandardColumn : DataGridViewColumn {
    public RICStandardColumn() : base(new RICStandardCell()) {
    }
}

// custom cell using a custom editor
public class RICStandardCell : DataGridViewCell {
   public override Type EditType { get { return typeof(RICStandardEditingControl); } }

   // optional override
   public override void InitializeEditingControl(Control editor, DataGridViewCellStyle style) {
    // initialize the editing control here.
    base.InitializeEditingControl(editor, style);
  }
}

// custom editor implementing IDataGridViewEditingControl
// if you extend an existing class like DataGridViewTextBoxEditingControl just call the base for the initialization.
public class RICStandardEditingControl : TextBox, IDataGridViewEditingControl {

  public DataGridView DataGridView { get; set; }

  public void ApplyCellStyleToEditingControl(DataGridViewCellStyle style) {
  }

  public string GetEditingControlFormattedValue() {
   return this.Text;
  }

  public void PrepareEditingControlForEdit(bool selectAll) {
  }

  protected override void OnTextChanged(EventArgs e) {
    base.OnTextChanged(e);

    // this is necessary to inform the grid that something has been edited or
    // the cell is never dirty.

    // ONLY when implementing IDataGridViewEditingControl, otherwise Wisej
    // will automatically use TextChanged for light implementation simply assigning the Editor property
    // of a column. Cannot mix the two.

    this.DataGridView?.NotifyCurrentCellDirty(true);
  }
}


  • Sascha Lorenz
    Thank you. I wasn’t aware that it gets so much easier, if you just use Controls instead of Editing Controls. With your help my current solution is reducing my Custom Columns and Cells to the bare minimum (because there is some other stuff in there I want to keep) and setting the Editor in the constructor to a Control that extends TextBox and not EditingControl. I will have CheckBoxes which need to save strings or numbers, so I assume I can use the properties “FalseValue” and “TrueValue” for that. I don’t really understand what you mean with your last comment in your code. I understand that why it needs to be set dirty, but what exactly can’t be mixed?
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.