2013-09-30 31 views
0

我有一個Web應用程序,其中顯示項目列表(listItem)。對每個元素,我分配它的文本和一個值。將文本和值都設置爲WFA中的ComboBox項目

我可以通過使用SelectedValue檢索值。

我現在正在將此網頁構建爲WFA,到目前爲止,我只能將文本分配給每個組合框項目。

我想補充一個值,它(這將是從數據庫的ID),所以後來我可以使用該值有效地更新/刪除等

會如何你們去嗎?

感謝

+0

如果您將'Web'應用程序移植到'Windows'應用程序,我強烈建議使用WPF而不是winforms。 winforms是一種非常古老的技術,沒人在意,而創建.Net Windows應用程序的首選和默認選項(按照[Microsoft官方文檔](http://www.microsoft.com/learning/en-us/companion) -moc.aspx))目前是WPF。 WPF範式比傳統的Winforms方法更接近Web範例。 –

回答

0

,你是用來的屬性是不存在的WinForms,但自從ComboBox需要一個對象,你可以用你需要的屬性使自己的自定義類。我已將ListControl.DisplayMember Property上的MSDN文檔作爲示例進行了修改。

它能做什麼是創建一個名爲customComboBoxItemTextValue屬性自定義類,然後我做一個列表並將其指定爲您ComboBoxDataSource分配Text屬性爲將DisplayMember。看看這對你是否可行。

public partial class Form1 : Form 
{ 
    List<customComboBoxItem> customItem = new List<customComboBoxItem>(); 

    public Form1() 
    { 
     InitializeComponent(); 
     customItem.Add(new customComboBoxItem("text1", "id1")); 
     customItem.Add(new customComboBoxItem("text2", "id2")); 
     customItem.Add(new customComboBoxItem("text3", "id3")); 
     customItem.Add(new customComboBoxItem("text4", "id4")); 
     comboBox1.DataSource = customItem; 
     comboBox1.DisplayMember = "Text"; 
     comboBox1.ValueMember = "Value"; 

    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     MessageBox.Show(((customComboBoxItem)comboBox1.SelectedItem).Text + " " 
         + ((customComboBoxItem)comboBox1.SelectedItem).Value); 
    } 
} 

public class customComboBoxItem 
{ 
    private string text; 
    private string value; 

    public customComboBoxItem(string strText, string strValue) 
    { 
     this.text = strText; 
     this.value = strValue; 

    } 

    public string Text 
    { 
     get { return text; } 
    } 

    public string Value 
    { 
     get { return value; } 
    } 

} 
+0

謝謝!!!!!!!! –

相關問題