lookupedit get selected value

14,809

Solution 1

I am not sure what you are asking about, but the easiest way to get selected value is like this:

public partial class Form1 : Form
{
    public class Example
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

    public List<Example> elist = new List<Example>();

    public Form1()
    {
        InitializeComponent();
        for (int i = 0; i < 10; i++)
        {
            elist.Add(new Example() { Id = i, Name = "Name" + i, Description = "Description " + i });
        }
        lookUpEdit1.Properties.DataSource = elist;
        lookUpEdit1.Properties.DisplayMember = "Name";
    }

    private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
    {
        var item = lookUpEdit1.GetSelectedDataRow() as Example;
    }
}

Solution 2

A small addition to the accepted answer. The solution proposed doesn't return the selected row, it returns the first row where the value of the field bound to ValueMember matches EditValue (which in the majority of cases happens to be the selected row however).

This is a problem when you f.ex have a multi-column LookUpEdit where a composite of two or more columns make a row unique. I know this is a bit out of the ordinary though...

Share:
14,809

Related videos on Youtube

Edgar
Author by

Edgar

Updated on June 04, 2022

Comments

  • Edgar
    Edgar almost 2 years

    I'm using devexpress, and visualstudio 2010. I have LookUpEdit control, where i want to choose value, but display it with the specified format: there is example which i used with buttonEdit:

        CurrentEvent.fkVersion = selectedVersion;
        m_cVersionButtonEdit.EditValue= CurrentEvent.fkVersion.FormattedProduct;
        m_cVersionButtonEdit.Refresh(); 
    

    "selectedVersion" is an object which i choose in dialog after button press.

    now I have to do the same, but using lookupEdit and selecting version from dropDownlist. So question is how to get Selected value?

  • Lukasz Szczygielek
    Lukasz Szczygielek over 3 years
    How your answer address an issue from question?
  • pensum
    pensum over 3 years
    When providing an answer, your goal is to try and explain why your answer is a good solution. You also need to take account that your answer will probably be read in the future by people that has the same issue as the OP, but they won't necessarily have the same context. I suggest you put more effort in your answers so that others can upvote you when you are right and explain well.