سؤال

I have 2 LookUpEdit controls from DevExpress on my form. Both use an ObservableCollection as it's datasource, one being of type string and the other of type double. The LookUpEdit control has an event called ProcessNewValue which fires when, you guessed it, a new value is entered in the control. I've added some code in this event to add the newly added value to the ObservableCollection and it automatically selects it once done. This works as expected for the string LooUpEdit but when I try it with the double LookUpEdit`, it adds it to the collection but then it clears out the control.

Here's the code to load the controls, which gets called in Form_Load():

    void InitControls()
    {
        double[] issueNumbers = new double[5];

        issueNumbers[0] = 155;
        issueNumbers[1] = 156;
        issueNumbers[2] = 157;
        issueNumbers[3] = 158;
        issueNumbers[4] = 159;

        ObservableCollection<double> issues = new ObservableCollection<double>(issueNumbers);

        lookupIssues.Properties.DataSource = issues;
        DevExpress.XtraEditors.Controls.LookUpColumnInfoCollection colInfo = lookupIssues.Properties.Columns;
        colInfo.Clear();
        colInfo.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Column"));
        colInfo[0].Caption = "Issue ID's";

        string[] stringNumbers = Array.ConvertAll<double, string>(issueNumbers, Convert.ToString);
        ObservableCollection<string> issuesString = new ObservableCollection<string>(stringNumbers);

        lookupStringValue.Properties.DataSource = issuesString;
        colInfo.Clear();
        colInfo.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Column"));
        colInfo[0].Caption = "String Issue ID's";

    }

And here's the ProcessNewValue event for both (I've renamed them to try to make it easier to see which does what):

    private void OnProcessNewValue_Double(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
    {
        ObservableCollection<double> source = (ObservableCollection<double>)(sender as LookUpEdit).Properties.DataSource;

        if (source != null)
        {
            if ((sender as LookUpEdit).Text.Length > 0)
            {
                source.Add(Convert.ToDouble((sender as LookUpEdit).Text));
                (sender as LookUpEdit).Refresh();
            }
        }

        e.Handled = true;
    }

    private void OnProcessNewValue_String(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
    {
        ObservableCollection<string> source = (ObservableCollection<string>)(sender as LookUpEdit).Properties.DataSource;

        if (source != null)
        {
            if ((sender as LookUpEdit).Text.Length > 0)
            {
                source.Add((sender as LookUpEdit).Text);
                (sender as LookUpEdit).Refresh();
            }
        }

        e.Handled = true;
    }

As you can see, the code it identical with the exception of one converting text to a double before adding it to the collection.

Anyone know why the double value gets added to the collection but the control doesn't automatically select it like it does with a string collection? I've even tried to hard-code the newly added value right after e.Handled = true; but it still doesn't select it. What's weird is that if I run it through the debugger, I can step through and see that the lookupIssues control indeed gets the newly added value AND it's Text property is set to it, but as soon as the event terminates, the control clears it out.....really strange.

Any help is greatly appreciated!

BTW, I can add a link to a sample project that duplicates the problem but you would need to have DevExpress v12.2.6 controls installed in order to compile the project.

هل كانت مفيدة؟

المحلول

I posted this to the DevExpress team as well and they were gracious enough to provide the solution:

I agree that this discrepancy appears confusing as-is. The reason for the discrepancy is LookUpEdit.ProcessNewValueCore makes a call to RepositoryItemLookUpEdit.GetKeyValueByDisplayValue which returns a null value from the LookUpListDataAdapter because no implicit conversion exists from double to string. You may resolve the discrepancy with the following change to your ProcessNewValue handler:

private void OnProcessNewValue_Double(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e) 
{
    ObservableCollection<double> source = (ObservableCollection<double>)(sender as LookUpEdit).Properties.DataSource;

    if (source != null) {
        if ((sender as LookUpEdit).Text.Length > 0) {
            double val = Convert.ToDouble((sender as LookUpEdit).Text);
            source.Add(val);
            e.DisplayValue = val;
            (sender as LookUpEdit).Refresh();
        }
    }        
    e.Handled = true;
}

The control now behaves as expected. I hope this can help someone else out :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top