RepositoryItemLookupEdit new value disappears before being processed

1.9k Views Asked by At

I have a LookupEdit in the grid that needs to be able to accept new values. When I enter the new value and press "Enter" or "Tab", it saves normally via the ProcessNewValue event, however when I enter a value and click elsewhere in the grid, on another cell or just in white space, the value vanishes completely. By implementing several other events and setting breakpoints all I figured out was that the value disappears before the "CloseUp" event fires. Validating, EditValueChanged, EditValueChanging, ProcessNewValue, Closed, Leave, and even GetNotInListValue never even get called because of the empty value.

Can anyone think of some setting I haven't found yet, or any other reason why this value would disappear...And how I might stop it from happening?

Found a valid Workaround

I implemented the following 3 events, in sequence, to solve this issue. I still have no idea what caused it, or how to go about preventing it. This is a Workaround, not a solution, and should be treated as such. I end up having to manually call the ProcessNewValue method, as well as forcing the value to equal the text field, and the text field back into the value later on. Not the smoothest of operations, but it does work.

private void repPatchNum1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
    {
        string surfaceSoftware = vwSurfaceSoftware.GetRowCellDisplayText(vwSurfaceSoftware.FocusedRowHandle, "SurfaceSoftware");
        if (string.Compare(surfaceSoftware, SOFTWARE_CHECK, true) == 0)
        {
            string version = vwSurfaceSoftware.GetRowCellDisplayText(vwSurfaceSoftware.FocusedRowHandle, "Version");
            if (version.ToLower().Contains(VERSION_CHECK))
            {//now we are certain we are in the right place
                LookUpEdit editor = sender as LookUpEdit;
                if (!((RPickListCollection)((BindingSource)editor.Properties.DataSource).DataSource).OfType<RPickList>().Any(a => a.RValue.Equals(e.NewValue)))
                {
                    repPatchNum1_ProcessNewValue(sender, new DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs(e.NewValue));
                    vwSurfaceSoftware.SetRowCellValue(vwSurfaceSoftware.FocusedRowHandle, colPatchNum, e.NewValue);
                }
            }
        }
    }

    private void repPatchNum1_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e)
    {
        string surfaceSoftware = vwSurfaceSoftware.GetRowCellDisplayText(vwSurfaceSoftware.FocusedRowHandle, "SurfaceSoftware");
        if (string.Compare(surfaceSoftware, SOFTWARE_CHECK, true) == 0)
        {
            string version = vwSurfaceSoftware.GetRowCellDisplayText(vwSurfaceSoftware.FocusedRowHandle, "Version");
            if (version.ToLower().Contains(VERSION_CHECK))
            {//now we are certain we are in the right place
                LookUpEdit editor = sender as LookUpEdit;
                if (!((RPickListCollection)((BindingSource)editor.Properties.DataSource).DataSource).OfType<RPickList>().Any(a => a.RValue.Equals(e.Value)))
                {
                    e.Value = ((LookUpEdit)sender).Text;
                }
            }
        }
    }

    private void repPatchNum1_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
    {
        string surfaceSoftware = vwSurfaceSoftware.GetRowCellDisplayText(vwSurfaceSoftware.FocusedRowHandle, "SurfaceSoftware");
        if (string.Compare(surfaceSoftware, SOFTWARE_CHECK, true) == 0)
        {
            string version = vwSurfaceSoftware.GetRowCellDisplayText(vwSurfaceSoftware.FocusedRowHandle, "Version");
            if (version.ToLower().Contains(VERSION_CHECK))
            {//now we are certain we are in the right place
                LookUpEdit editor = sender as LookUpEdit;
                string patch = vwSurfaceSoftware.GetRowCellValue(vwSurfaceSoftware.FocusedRowHandle, colPatchNum).ToString();
                if (String.IsNullOrEmpty(editor.Text) && !String.IsNullOrEmpty(patch))
                {
                    editor.Text = vwSurfaceSoftware.GetRowCellValue(vwSurfaceSoftware.FocusedRowHandle, colPatchNum).ToString();
                    vwSurfaceSoftware.UpdateCurrentRow();
                }
            }
        }
    }

As to the original question: Please post an answer if you know why this might be happening or how to prevent it.

Thanks all :-)

1

There are 1 best solutions below

1
Dejan Grujić On

I think I found a simpler workaround, tested on DevExpress 13.

When user presses Tab/Enter, event sequence is ProcessNewValue -> CloseUp

However if user finishes lookup by clicking somewhere else, events are reversed: CloseUp -> ProcessNewValue and entered value is lost. We can use PopupCloseMode.Immediate (specifies that the dropdown window was closed because an end-user clicked outside the editor) to detect this case, manually take entered value from editor, set it to event Value field and manually call ProcessNewValue. No need for other events.

private void myLookUp_CloseUp( object sender, CloseUpEventArgs e )
{
    var lookUpEdit = sender as LookUpEdit;
    if( lookUpEdit != null )
    {
        var enteredLookUpText = lookUpEdit.Text;
        if( e.CloseMode == PopupCloseMode.Immediate )
        {
            e.Value = enteredLookUpText;
            myLookUp_ProcessNewValue( sender, new ProcessNewValueEventArgs( enteredLookUpText ) );
        }
    }
    // Rest of event handler
}