简体   繁体   English

如何从ComboBox C#获取ValueMember值?

[英]How to get ValueMember value from ComboBox C#?

I'm having some trouble trying to get the ValueMember value I've set. 我在尝试获取设置的ValueMember值时遇到了一些麻烦。 I'm trying to use a combobox to read XML data to textbox. 我正在尝试使用组合框将XML数据读取到文本框。 Here's my code: 这是我的代码:

    private void zamowienie_Load(object sender, EventArgs e)
    {
        label2.Text = DateTime.Now.ToShortDateString();

        DataSet dsSet = new DataSet();
        dsSet.ReadXml("E:\\baza\\spis_klientow.xml");
        comboBox2.DataSource = dsSet.Tables["spis_klientow"];
        comboBox2.DisplayMember = "ID";
        comboBox2.ValueMember = "Name";
        comboBox2.ValueMember = "Phone";

    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {            
        DataSet ds = new DataSet();
        ds.ReadXml("E:\\baza\\spis_klientow.xml");

        foreach (DataRow item in ds.Tables["spis_klientow"].Rows)
        {
            label10.Text = item[1].ToString();
            label11.Text = item[2].ToString();
        }

    }

When you bind a DataTable as ComboBox data source, the ComboBox.Items collection is populated with DataRowView objects. 当您将DataTable绑定为ComboBox数据源时, ComboBox.Items集合将填充DataRowView对象。 Then you can use ComboBox.SelectedItem like this 然后您可以像这样使用ComboBox.SelectedItem

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    var source = (DataRowView)comboBox2.SelectedItem;
    label10.Text = source["Name"].ToString();
    label11.Text = source["Phone"].ToString();
    // ...
}

However, there is a better way. 但是,有更好的方法。 WinForms data binding infrastructure supports such scenario without a need to hook into a concrete control events. WinForms数据绑定基础结构支持这种情况,而无需陷入具体的控制事件。 All you need is to bind combo box and text boxes to one and the same data source ( DataTable in your case). 您所需要做的就是将组合框和文本框绑定到一个相同的数据源(本例中为DataTable )。

Here is a full working example of the data binding "magic" in action: 这是实际运行中的数据绑定“魔术”的完整示例:

using System;
using System.Data;
using System.Windows.Forms;

namespace Samples
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var form = new Form();
            var comboBox = new ComboBox { Left = 16, Top = 16, DropDownStyle = ComboBoxStyle.DropDownList };
            var textBoxID = new TextBox { Left = 16, Top = comboBox.Bottom + 8 };
            var textBoxName = new TextBox { Left = 16, Top = textBoxID.Bottom + 8 };
            var textBoxPhone = new TextBox { Left = 16, Top = textBoxName.Bottom + 8 };
            form.Controls.AddRange(new Control[] { comboBox, textBoxID, textBoxName, textBoxPhone });

            // Begin essential part
            var dataSource = GetData();

            textBoxID.DataBindings.Add("Text", dataSource, "ID");
            textBoxName.DataBindings.Add("Text", dataSource, "Name");
            textBoxPhone.DataBindings.Add("Text", dataSource, "Phone");

            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "ID";
            comboBox.DataSource = dataSource;

            // End essential part
            Application.Run(form);
        }

        static DataTable GetData()
        {
            //DataSet dsSet = new DataSet();
            //dsSet.ReadXml("E:\\baza\\spis_klientow.xml");
            //return dsSet.Tables["spis_klientow"];

            var table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name");
            table.Columns.Add("Phone");
            for (int i = 1; i <= 10; i++)
                table.Rows.Add(i, "Name" + i, "Phone" + i);
            return table;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM