tags:

views:

626

answers:

1

I have some lookupedits binded to some lists where the user can choose values and then save in database. I use EditValueChanged events to handle the values. So far all good!

Now i need to grab the values from the database and assign them to the lookupedits. I don't use BindingSource for the whole object cause lookupedits are binded to independent lists. As i supposed and read from the documentation, SelectedText is what i need, but when I'm assigning the string i want, it just don't work and sets an empty string. Same Behavior for the DateEdit control, I'm assigning a DateTime value and seems to have this value but doesn't shows it. I could set the EditValue property but i get nothing showed up in the LookUpEdit again.

How to force the LookUpEdit to show me the value i want, basically go to the row with the value i set and show the text in the editor too, or set the SelectedText and match it with its list and show it!

I guess this should be easier...Any help appreciated!

Example:

myLookUpEdit.SelectedText = "George" // The LookUpEdit is Binded to a List<Names> and has the name George.

Thank you

+1  A: 

Whenever I am setting the value of a LookupEdit I use EditValue.

You need to make sure that you set the ValueMember property of the LookupEdit to whatever you want to appear in EditValue. DisplayMember will what is displayed when the LoodupEdit is closed. You can pass in a string to the name of the property you want in your object to these properties.

Setting the SelectedText value has the same effect as typing into the control as far as I am aware.

public partial class Form1 : Form
{
    List<Name> MyNames = new List<Name>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        MyNames.Add(new Name("John", "Smith"));
        MyNames.Add(new Name("John", "Doe"));
        MyNames.Add(new Name("Jane", "Doe"));
        MyNames.Add(new Name("Jane", "Smith"));

        lookUpEdit1.Properties.DataSource = MyNames;
        lookUpEdit1.Properties.DisplayMember = "FirstName";
        lookUpEdit1.Properties.ValueMember = "FirstName";

    }

    private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
    {

        string mystring = lookUpEdit1.EditValue.ToString();

        lookUpEdit1.EditValue = mystring;
    }
}

public class Name
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Name(string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
    }
}
csjohnst
Thank you for your time, i have fixed that :)