gökhan
gökhan

Reputation: 13

LookUpEdit does not work

I have XtraTabControl with two pages, both of them has one LookUpEdit, when page load which on the secondpage does not work,

void Frm1_Load(object sender, EventArgs e)
{
    lookUpEditA.Properties.DataSource = datasource. . . . .
    lookUpEditA.Properties.ValueMember = "ID";
    lookUpEditA.Properties.DisplayMember = "xxxx";
    lookUpEditA.Properties.PopulateColumns();
    lookUpEditA.Properties.Columns["ID"].Visible = false;

    lookUpEditB.Properties.DataSource = datasource. . . . .
    lookUpEditB.Properties.ValueMember = "ID";
    lookUpEditB.Properties.DisplayMember = "xxxx";
    lookUpEditB.Properties.PopulateColumns();
    lookUpEditB.Properties.Columns["ID"].Visible = false;
}

Upvotes: 1

Views: 1914

Answers (2)

Niranjan Singh
Niranjan Singh

Reputation: 18290

As @DmitryG said, you can not use lookUpEditB.Properties.PopulateColumns() statement until control's UI handlers are not created.

As per my understanding these are created only when the second tab page shown. Except creating conditional statement to create handlers etc, you can use the XtraTabControl.SelectedPageChanged Event where you can bind the data source of the lookUpEditB by checking condition that XtraTabControl.SelectedTabPage Property is set with Page2 which contain the lookUpEditB.

Check the tested Code Snippet below:

public partial class TabControlTest : Form
{
    List<Category> dataSource = new List<Category>();
    public TabControlTest()
    {
        InitializeComponent();
        for (int i = 0; i < 10; i++)
        {
            dataSource.Add(new Category { ID = i + 1, Name = "Category" + (i + 1) });
        }
    }

    private void TabControlTest_Load(object sender, EventArgs e)
    {
        lookUpEditA.Properties.DataSource = dataSource;
        lookUpEditA.Properties.ValueMember = "ID";
        lookUpEditA.Properties.DisplayMember = "Name";
        lookUpEditA.Properties.PopulateColumns();
        lookUpEditA.Properties.Columns["ID"].Visible = false;
    }

    private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
    {
        if (xtraTabControl1.SelectedTabPage == xtraTabPage2)
        {
             lookUpEditB.Properties.DataSource = dataSource;
            lookUpEditB.Properties.ValueMember = "ID";
            lookUpEditB.Properties.DisplayMember = "Name";
            lookUpEditB.Properties.PopulateColumns();
            lookUpEditB.Properties.Columns["ID"].Visible = false;
        }
    }
}

Hope this help.

Upvotes: 0

DmitryG
DmitryG

Reputation: 17850

I can see the issue only with setting visibility of 'ID' column on second LookUpEdit.

The reason of this issue is that the LookUpEdit can't operate with datasource representation (perform populating columns, operating with column's visibility and etc.) until it's handle been created. The second LookUpEdit will create it's handle only when the second tab page has been shown.

To avoid the issue you can use the following approach:

if(!lookUpEditB.IsHandleCreated)
    lookUpEditB.HandleCreated += lookUpEditB_HandleCreated;
else InitLookUpEditDataSource();
//...
void lookUpEditB_HandleCreated(object sender, EventArgs e) {
    lookUpEditB.HandleCreated -= lookUpEditB_HandleCreated;
    InitLookUpEditDataSource();
}
void InitLookUpEditDataSource() {
    lookUpEditB.Properties.DataSource = this.categoriesBindingSource;
    lookUpEditB.Properties.DisplayMember = "CategoryName";
    lookUpEditB.Properties.ValueMember = "CategoryID";
    lookUpEditB.Properties.PopulateColumns();
    lookUpEditB.Properties.Columns["CategoryID"].Visible = false;
}

Upvotes: 1

Related Questions