How to Hide Dictionary Key in LookupEdit using C#?

4.1k Views Asked by At

I created a normal winform and added LookUpEdit to my form and created a dictionary which contains string keys and string values. I loaded my LookupEdit.Properties.Datasource using the BindingSource.
When Lookupedit is loaded, I want to hide dictionary Key:

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

This shows the following:

Output 
Test  1
Test2 2

I need Output as '1'
'Test' has to be hide.

2

There are 2 best solutions below

2
Rajeev Kumar On

Manage the following event in code behind

lookup1.Popup += new EventHandler(gridLookUpEdit1_Popup);

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

Solution: Just add two columns to lookupEdit and then assign datasource. after that you will be able to access these columns. Dynamically generated columns does not added to lookupEdit.properties.Columns collection.

Refer LookUpEdit Class

Use this:

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

References:
Dictionary as datasource
LookupEdit bound to ArrayList
Feed lookupEdit with Dictionary