填充combox并在组合框中显示值

时间:2011-03-13 22:34:29

标签: c# winforms

填充combox并在组合框中显示值 例如。如果我在组合框中选择名称,相应的ID应显示在文本框中。有人知道吗?我正在学习c#。我试过它可以任何人检查以下代码。使用系统;

我想创建一个存储值的数组,然后在文本框中写出来。任何人都可以帮忙

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace DBExample
{
    public partial class Form1 : Form
    {
        private OleDbConnection dbConn; // Connectionn object
        private OleDbCommand dbCmd; // Command object
        private OleDbDataReader dbReader;// Data Reader object
        private Member aMember;
        private string sConnection;
        // private TextBox tb1;
        // private TextBox tb2;
        private string sql;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // Construct an object of the OleDbConnection 
                // class to store the connection string 
                // representing the type of data provider 
                // (database) and the source (actual db)
                sConnection =
                        "Provider=Microsoft.Jet.OLEDB.4.0;" +
                        "Data Source=c:member.mdb";
                dbConn = new OleDbConnection(sConnection);
                dbConn.Open();
                // Construct an object of the OleDbCommand 
                // class to hold the SQL query. Tie the 
                // OleDbCommand object to the OleDbConnection
                // object
                sql = "Select * From memberTable Order " +
                        "By LastName , FirstName ";
                dbCmd = new OleDbCommand();
                dbCmd.CommandText = sql;
                dbCmd.Connection = dbConn;
                // Create a dbReader object 
                dbReader = dbCmd.ExecuteReader();
                while (dbReader.Read())
                {
                    aMember = new Member
                                (dbReader["FirstName"].ToString(),
                                 dbReader["LastName"].ToString(),
                                 dbReader["StudentId"].ToString(),
                                 dbReader["PhoneNumber"].ToString());
                    // tb1.Text = dbReader["FirstName"].ToString();
                    // tb2.Text = dbReader["LastName"].ToString();
                    // tb1.Text = aMember.X().ToString();
                    //tb2.Text = aMember.Y(aMember.ID).ToString(); 
                    BindingList<Member> comboBoxList = new BindingList<Member>();

                    comboBox1.DataSource = comboBoxList; 
                    comboBox1.DisplayMember = "FirstName"; 
                    comboBox1.ValueMember = "ID";
                    // this.comboBox1.Items.Add("aMember.FirstName.ToString()");
                    // this.listBox1.Items.Add(aMember.ToString());
                    // MessageBox.Show(aMember.ToString());
                    // Console.WriteLine(aMember.ToString());
                }
                dbReader.Close();
                dbConn.Close();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show("show" + exc);
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Text = comboBox1.SelectedValue.ToString();
            this.textBox1.Text = comboBox1.SelectedItem.ToString();
        }


        private void DbGUI_Load(object sender, EventArgs e)
        {
        }



        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }
    }

}

1 个答案:

答案 0 :(得分:0)

你应该尝试这样的事情

BindingList<Member> comboBoxList = new BindingList<Member>();
while (dbReader.Read())
{
    aMember = new Member
                (dbReader["FirstName"].ToString(),
                 dbReader["LastName"].ToString(),
                 dbReader["StudentId"].ToString(),
                 dbReader["PhoneNumber"].ToString());
    // tb1.Text = dbReader["FirstName"].ToString();
    // tb2.Text = dbReader["LastName"].ToString();
    // tb1.Text = aMember.X().ToString();
    //tb2.Text = aMember.Y(aMember.ID).ToString(); 

    comboList.Add(aMember)

}

comboBox1.DataSource = comboBoxList; 
                        comboBox1.DisplayMember = "FirstName"; 
                        comboBox1.ValueMember = "ID";