1

我有两个页面的 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;
}
4

2 回答 2

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;
}
于 2013-01-18T08:52:40.027 回答
0

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

据我了解,这些仅在显示第二个标签页时创建。除了创建条件语句来创建处理程序等之外,您可以使用XtraTabControl.SelectedPageChanged Event您可以lookUpEditB通过检查使用Page2XtraTabControl.SelectedTabPage Property设置的包含.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;
        }
    }
}

希望这有帮助。

于 2013-01-18T10:43:05.830 回答