且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Access数据集/数据库填充组合框

更新时间:2023-11-27 10:12:16

假设您的表单具有以下内容:

Assuming you have a Form with:


  1. 名为cboName的组合框

  2. 名为txtName的文本框

尝试一下:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            LoadCustomerOnCombo();
        }

        private void LoadCustomerOnCombo()
        {
            string strCon = Settings.Default.PID2dbConnectionString;

            try
            {
                using (OleDbConnection conn = new OleDbConnection(strCon))
                {
                    conn.Open();
                    string strSql = "SELECT forename &\" \" & surname AS FullName, surname FROM customer"; //WHERE [customerID] ='" + txtName.Text + "'";
                    OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(strSql, conn));
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    cboName.DataSource = ds.Tables[0];
                    cboName.DisplayMember = "FullName";
                    cboName.ValueMember = "surname";
                }     
            }
            catch (Exception ex)
            {
                txtName.Text = ex.Message;
                Console.WriteLine(ex.Message);
            }
        }
    }

如果可行,请告诉我如何
,在此处留下评论的位置,否则有任何错误,您会在文本框内看到。

If worked so please tell me how is the where, left commented at the moment, else there are any errors, you'll see inside the textbox.