0

アイテムのリスト (listItem) を表示する Web アプリケーションがあります。各要素に、そのテキストと値を割り当てます。

SelectedValue を使用して値を取得できます。

現在、この Web ページを WFA として作成していますが、これまでのところ、テキストを各コンボ ボックス アイテムに割り当てることしかできません。

それに値(データベースからのIDになります)を追加したいので、その値を使用して効率的に更新/削除などを行うことができます.

皆さんはどうしますか?

ありがとう

4

1 に答える 1

0

使い慣れたプロパティは Winforms にはありませんが、ComboBoxはオブジェクトを受け取るため、必要なプロパティを使用して独自のカスタム クラスを作成できます。ListControl.DisplayMember プロパティに関する MSDN ドキュメントを参考にして、例として修正しました。

それが行うことは、プロパティで呼び出さcustomComboBoxItemれるカスタム クラスを作成することです。次に、リストを作成し、それをプロパティを DisplayMember として割り当てます。これが実行可能かどうかを確認してください。TextValueDataSourceComboBoxText

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

}
于 2013-09-30T01:53:06.433 に答える