0

I have a Winforms application which allows the user to edit an array of objects. An object is selected by clicking on a row in a DataGridView and databound to various controls.
Recently, I wanted the object to be able to handle null values (for fields without data yet). This was accomplished using the type? convention.

private PDAFiles? pdafile;
public new PDAFiles? PDAFile
{
    get { return this.pdafile; }
    set { this.pdafile = value; }
}

Some additional handling (through custom formatting) allows fields to be read and wrote correctly. However, entering a value into a control locks up the GUI.

My code worked as intended before implementing nullable type? types. Looking at the variables in a debugger reveals that all values are read/written correctly. The program throws no exceptions. Most controls, including the close button, cannot be interacted with. The DataGridView, which selects an object to bind, can be interacted with, and selecting a different object/adding an object restores functionality.

DataBinding on ComboBox control:

Binding binding = new Binding("SelectedIndex", this.current_criteria, "PDAFile", true);
binding.Format += (sender, e) =>
{
    try
    {
        e.Value = e.Value == null ? -1 : e.Value;
    }
    catch { }
};
this.signalFileBox.DataBindings.Add(binding);

Removing these lines causes the problem to disappear. However, as the control is no longer databound, it does not load existing values from the object. A separate binding parses the user input. Removing the parser has no effect on locking up the GUI.

Setting DataSource from DataGridView:

this.selected_row_index = this.dataGridViewCriteria.CurrentCell.RowIndex;
MultiDataCollectionCriteria sources =
    new MultiDataCollectionCriteria
    (
        (from row in this.dataGridViewCriteria.SelectedRows.Cast<DataGridViewRow>() 
        select this.data_criteria_source[row.Index] as DataCollectionCriteria).ToArray()
    );
this.current_criteria.DataSource = sources;
this.criteriaPanel.Enabled = true;

These lines function as intended; however, as noted, selecting a new row from the DataGridView unlocks the GUI.

I am looking to be able to bind controls to nullable types (with formatting) without causing the GUI to lock up. The behavior would be identical to a normal DataBinding, except that it can handle type? sources.

Groger
  • 532
  • 3
  • 15

1 Answers1

0

A similar question was posed here
Best way to databind a Winforms control to a nullable type?

The problem was solved by changing the Binding to Binding binding = new Binding("SelectedIndex", this.current_criteria, "PDAFile", true, DataSourceUpdateMode.Never, -1);

The DataSourceUpdateMode.Never was particularly important, as opposed to DataSourceUpdateMode.OnPropertyChanged, since I was manually parsing the input through separate means.

Groger
  • 532
  • 3
  • 15