如何使用C#在LookupEdit中隐藏字典键?

时间:2013-02-06 11:28:49

标签: c# devexpress repositorylookupedit

我创建了一个普通的winform并将LookUpEdit添加到我的表单中,并创建了一个包含string个键和string值的字典。我使用LookupEdit.Properties.Datasource加载了BindingSource 当加载Lookupedit时,我想隐藏字典密钥:

private LookUpEdit lookup1;

void InitializeComponent()
{
    //...
    this.lookup1 = new DevExpress.XtraEditors.LookUpEdit();
    this.lookup1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
    //this.cmbCards.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.lookup1.Location = new System.Drawing.Point(400, 125);
    this.lookup1.Name = "Test";
    this.lookup1.Properties.ShowHeader = false;
    this.lookup1.Properties.ValueMember = "Test";
    this.lookup1.Size = new System.Drawing.Size(400, 85);
    this.lookup1.TabIndex = 0;
    this.lookup1.Tag = "";
    this.lookup1.Properties.BestFit();
    this.lookup1.Properties.ShowDropDown = DevExpress.XtraEditors.Controls.ShowDropDown.SingleClick;
    this.lookup1.Properties.BestFit();
    this.lookup1.Properties.PopupWidth = 50;
    this.lookup1.Properties.PopupSizeable = false;
    //...
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("Test", "1");
        dic.Add("Test2", "2");
        dic.Add("Test3", "3");
        dic.Add("Test4", "4");
        dic.Add("Test5", "5");
        dic.Add("Test6", "6");
        dic.Add("Test7", "7");
        dic.Add("Test8", "8");
        dic.Add("Test9", "9");
        dic.Add("Test10", "10");

        this.lookup1.Properties.DataSource = new BindingSource(dic, null);
        this.lookup1.Properties.ShowLines = false;
        this.lookup1.Properties.ShowPopupShadow = false;
        this.lookup1.ItemIndex = 0;
    }
}

这显示以下内容:

Output 
Test  1
Test2 2

我需要输出为'1' “测试”必须隐藏。

2 个答案:

答案 0 :(得分:2)

<强>解决方案: 只需向lookupEdit添加两列,然后分配datasource即可。之后,您将能够访问这些列。动态生成的列不会添加到lookupEdit.properties.Columns集合中。

参考LookUpEdit Class

使用此:

private void CreateLookupEdit()
{
    ledMyControl = new LookUpEdit();
    ledMyControl.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Key"));
    ledMyControl.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Value"));

    this.Controls.Add(ledMyControl);
    Dictionary<string, string> dic = new Dictionary<string, string>();
    dic.Add("Test", "1");
    dic.Add("Test2", "2");
    dic.Add("Test3", "3");
    dic.Add("Test4", "4");
    dic.Add("Test5", "5");
    dic.Add("Test6", "6");
    dic.Add("Test7", "7");
    dic.Add("Test8", "8");
    dic.Add("Test9", "9");
    dic.Add("Test10", "10");
    ledMyControl.Properties.DisplayMember = "Value";
    ledMyControl.Properties.ValueMember = "Key";
    ledMyControl.Properties.DataSource = dic.ToList();

    ledMyControl.Properties.Columns[0].Visible = false;
}

参考文献:
Dictionary as datasource
LookupEdit bound to ArrayList
Feed lookupEdit with Dictionary

答案 1 :(得分:1)

背后的代码中管理以下事件
lookup1.Popup += new EventHandler(gridLookUpEdit1_Popup);

protected void gridLookUpEdit1_Popup(object sender, EventArgs e)
{
    this.lookup1.Properties.View.Columns[0].Visible = false;
}