LookUpEdit不起作用

时间:2013-01-17 20:03:01

标签: c# winforms devexpress lookup

我有两个页面的XtraTabControl,它们都有一个LookUpEdit, 当第二页上的页面加载不起作用时,

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;
}

2 个答案:

答案 0 :(得分:1)

我只能在第二个LookUpEdit上设置“ID”列的可见性时才能看到问题。

此问题的原因是LookUpEdit无法使用数据源表示(执行填充列,使用列的可见性等操作),直到创建了句柄。只有在显示第二个标签页时,第二个LookUpEdit才会创建它的句柄。

要避免此问题,您可以使用以下方法:

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;
}

答案 1 :(得分:0)

正如@DmitryG所说,在创建控件的UI处理程序之前,不能使用lookUpEditB.Properties.PopulateColumns()语句。

根据我的理解,这些仅在显示第二个标签页时创建。除了创建条件语句以创建处理程序等之外,您可以使用XtraTabControl.SelectedPageChanged Event来绑定lookUpEditB的数据源,方法是检查XtraTabControl.SelectedTabPage Property设置为 Page2的条件包含lookUpEditB

检查下面的测试代码段:

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;
        }
    }
}

希望得到这个帮助。