为什么我的自定义lookupEdit会抛出以下错误

时间:2011-09-14 21:43:52

标签: c# .net devexpress outofrangeexception repositorylookupedit

错误:索引超出范围必须是非负数且小于集合的大小

我有一个自定义的LookUpEdit -

public class MyLookUpEdit : LookUpEdit

我参考以下链接将MyLookUpEdit添加到功能区。现在MyLookUpEdit在我的ribbonPageGroup - > Add Editor。

中可用

http://www.devexpress.com/Support/Center/KB/ViewKBIssue.aspx?kbid=A1237

这是MyLookUpEdit的InitializeControl方法:

public void InitializeControl(ICollection cache, string columnField1, string columnField2, string valueField, bool isMultiColumn, int searchColumn)
        {
            Properties.ForceInitialize();
            const int maxDropDownSize = 7;
            Properties.Columns.Clear();

        if(isMultiColumn)
        {
                Properties.Columns.AddRange(new[] {
                new LookUpColumnInfo(columnField1, columnField1, columnField1.Length*6, FormatType.None, "", true, HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None),
                new LookUpColumnInfo(columnField2, columnField2, 100, FormatType.None, "", true , HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None)});
                Properties.ShowHeader = true;
                Properties.PopupFormMinSize = new Size(0, 0);
                Properties.AppearanceDropDownHeader.TextOptions.HAlignment = HorzAlignment.Near;
                Properties.AutoSearchColumnIndex = searchColumn;
        }
        else
        {
                Properties.Columns.AddRange(new[]{
                new LookUpColumnInfo(columnField1, columnField1, 10, FormatType.None, "", true, HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None),
                new LookUpColumnInfo(columnField2, columnField2, 0, FormatType.None, "", false , HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None)});
                Properties.ShowHeader = false;
                Properties.PopupFormMinSize = new Size(10, 10); // set popup width to control width

                break;
        }

        Properties.ShowFooter = false;
        Properties.ShowLines = true;
        Properties.ValueMember = valueField;
        Properties.DisplayMember = columnField1;
        Properties.DataSource = cache;
        if (cache == null) return;
        Properties.DropDownRows = cache.Count > maxDropDownSize ? maxDropDownSize : cache.Count;

        ColumnBestFit();
        HideIcon();
    }

但是在运行时,在功能区中,我无法单击按钮并查看lookupedit的所有可用值 - 看起来弹出窗口未打开,当我离开编辑时,以下代码抛出错误:< / p>

    protected override void OnLeave(EventArgs e)
    {
        if (IsPopupOpen) 
        {
            ItemIndex = PopupForm.SelectedIndex;
            _selectfromPopup = true;
        }
    Properties.DisplayMember = Properties.Columns[0].Caption; // This line throw the error

        base.OnLeave(e);

        if (textChanged)
            OnLeaveWithChangedText(e);

    }

1 个答案:

答案 0 :(得分:1)

列列表为空可能有不同的原因,它取决于整个表单事件和方法链,但无论如何,但要确保使用防御方法(我的瑞士军刀;-))

之前只需检查一下:

Properties.DisplayMember = Properties.Columns[0].Caption;

如果Properties.Columns.Length > 0

,则执行该行

这不会解决所有问题,但正如我现在所理解的那样,会阻止该异常并且您的代码将继续运行(但如果错误位于列表中的其他位置,则最终并未真正完成您对它的期望不应该是空的。)