input string was not in a correct format. GenerateEditingElement combobox datagrid column

285 Views Asked by At

Exception: Input string was not in a correct format

I am not sure how i am getting this. It only happens when i try to re-enter the cell when a value is already placed. So if the user wants to edit what they previously entered it produces this exception.

I have this override so i can utilize the IsEditable property.

It produces the exception after the function GenerateEditingElement is run. If i skip over the code for debug i get this.

Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).

public class DataGridComboBoxColumn : DataGridBoundColumn {
    public Binding ItemsSourceBinding { get; set; }
    public string DisplayMemberPath { get; set; }
    public string SelectedValuePath { get; set; }
    public object ItemSource { get; set; }
    public int SelectedIndex { get; set; } = -1;

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) {
        var textBox = new TextBlock();
        if (Binding != null) {
            BindingOperations.SetBinding(textBox, TextBlock.TextProperty, Binding);
        }
        return textBox;
    }

    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) {
        var comboBox = new ComboBox { IsEditable = true };
        if (Binding != null && ItemsSourceBinding != null) {

            comboBox.DisplayMemberPath = DisplayMemberPath;
            comboBox.SelectedValuePath = SelectedValuePath;

            BindingOperations.SetBinding(comboBox, ComboBox.SelectedValueProperty, Binding);
            BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty, ItemsSourceBinding);

        }
        return comboBox;
    }

    protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) {
        var comboBox = editingElement as ComboBox;
        if (comboBox == null) return null;
        if (SelectedIndex != -1 ) { comboBox.SelectedIndex = SelectedIndex; } else { comboBox.SelectedIndex = -1; }
        comboBox.Focus(); // This solves the double-tabbing problem that Nick mentioned.
        return comboBox.Text;
    }

    protected override bool CommitCellEdit(FrameworkElement editingElement) {
        return base.CommitCellEdit(editingElement);
    }

    protected override void CancelCellEdit(FrameworkElement editingElement, object uneditedValue) {
        base.CancelCellEdit(editingElement, uneditedValue);
    }
}

XAML

<local:DataGridComboBoxColumn Header="Prod #" x:Name="cboProd" Width="60" Binding="{Binding ProductNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="ProductNum" SelectedValuePath="ProductsID" ItemsSourceBinding="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:PurMaterialEntry}}, Path=PDN}"/>

PDN is my collection of product numbers of the project in the combobox next to it.

What i am looking for.
- I want it to clear the cell on re-entry.
- The project ComboBox in the grid controls this one.

Image showing that the combobox works during entry.

But if i have to modify the product D101 the exception occurs.

0

There are 0 best solutions below