views:

740

answers:

2

I'm tearing my hair off for this amazing problem.

I'm binding 2 LookUpEdit from code:

            MyBinding.DataSource = typeof(MyObject);
        MyBinding.DataSource = _dataObject.GetMyList();

        firstLookUp.DataBindings.Add("EditValue", MyBinding, "Code");
        firstLookUp.Properties.DataSource = MyBinding;
        firstLookUp.Properties.ValueMember = "Code";
        firstLookUp.Properties.DisplayMember = "Code";

        secondLookUp.DataBindings.Add("EditValue", MyBinding, "Info");
        secondLookUp.Properties.DataSource = MyBinding;
        secondLookUp.Properties.ValueMember = "Info";
        secondLookUp.Properties.DisplayMember = "Info";

First problem is: Changing the value on one of the two LookUps not reflecting changing the other one! But im using the same BindingSource, isn't the position the same?

Another one is: They both populate automatically the columns, i dont want to show all columns, tried to remove, exception column not found, if i add, i get duplicate columns! I don't get it!!!

+1  A: 

Changing LookupEdit's EditValue is not directly bound to the BindingSource.Current position.

You have to use something like

lookUpEdit1.Properties.GetDataSourceRowByKeyValue(lookUpEdit1.EditValue)

If you want both LookupEdits linked you are probably better off setting the edit value of the one when the other is changed.

Secondly You should be able to clear the list of Columns like so:

lookUpEdit1.Properties.Columns.Clear();
lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("FirstName"));
csjohnst
So everything has to happen manually in code event. Thanx for reply.
Well, the columns should only be the ones you specify, the Clear() is only there because you said you were getting double ups. Normally the behavior I see is it shows all columns if you specify none and only the specified ones when you set them.
csjohnst
+1  A: 

As said here

http://www.devexpress.com/Support/Center/p/B138420.aspx

http://www.devexpress.com/Support/Center/p/A2275.aspx

LookupEdit does update the Current Property of the BindingSource.

We use the following Code as a workaround:

/// <summary>
/// Wrapper around DevExpress.XtraEditors.LookUpEdit to fix bug with adjusting the BindingSources Current Position
/// </summary>
public sealed class LookUpEditWithDataSource : LookUpEdit
{
    private bool firstCall = true;

    /// <summary>
    /// Called when the edit value changes.
    /// </summary>
    protected override void OnEditValueChanged()
    {
        base.OnEditValueChanged();

        if (this.Properties.DataSource == null)
        {
            return;
        }

        if (this.BindingContext == null)
        {
            return;
        }

        if (this.firstCall)
        {
            this.firstCall = false;

            // HACK
            // starting and selecting the first item
            // doesn't work so we change the position to the first item
            this.BindingContext[this.Properties.DataSource].Position = 1;
        }

        this.BindingContext[this.Properties.DataSource].Position = this.Properties.GetDataSourceRowIndex(this.Properties.ValueMember, this.EditValue);
    }
}
HumerGu
Thank you for the point.