2012-10-03 68 views
3

我使用的DevExpress,和VisualStudio的2010年 我有LookUpEdit控制,在這裏我要選擇的值,但與指定的格式顯示出來: 有例如我與buttonEdit使用:lookupedit得到選擇的值

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

「selectedVersion」是我在按鈕按下後在對話框中選擇的對象。

現在我必須這樣做,但使用lookupEdit並從dropDownlist中選擇版本。 所以問題是如何獲得選定的價值?

回答

5

我不知道你問什麼,但要獲得選擇的最簡單的方法值是這樣的:

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

一個小除了公認的答案。建議的解決方案不是返回選定的行,它返回綁定到ValueMember的字段的值與EditValue匹配的第一行(但在大多數情況下恰好是選定的行)。

這是一個問題,當你有一個多列LookUpEdit,其中兩列或多列的組合使一行是唯一的。我知道這有點不同尋常,但...