开发者

Combobox suggestions

开发者 https://www.devze.com 2023-02-14 03:08 出处:网络
I tried with the for each loop to populate the text box with studentid, when I choose the name from the combo-box. The problem is when I choose a name from the combo-box; it is not choosing the corres

I tried with the for each loop to populate the text box with studentid, when I choose the name from the combo-box. The problem is when I choose a name from the combo-box; it is not choosing the corresponding ID, and the ID is not changing. Do I have to use data tables?

using System;
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();  

                    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 DbGUI_Load(object sender, EventArgs e)
        {
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = comboBox1.SelectedItem.ToString();

            //textBox2.Text = string.Empty;
            foreach (var Item in comboBox1.Items);
                textBox2.Text += aMember.ID.ToString();

            //MessageBox.Show("read one record");
        }

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

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

The class file is as follows

using System;
namespace DBExample
{
    public class Member
    {
        private string id;
        private string firstName;
        private string lastName;
        private string phoneNumber;

        public Member()
            : this("Not", "Assigned", "No ID", "No Phone Service")
        {
        }

        // Constructor
        public Member(string firstname, string lastname, string studentid, string phonenumber)
        {
            this.firstName = firstname;
            this.lastName = lastname;
            this.id = studentid;
            this.phoneNumber = phonenumber;
        }

        public string FirstName
        {
            get
            {
                return firstName;
            }
        }

        public string LastName
        {
            get
            {
                return lastName;
            }
        }

        public string ID
        {
            get
            {
                return id;
            }
        }

        public string PhoneNumber
        {
            get
            {
                return phoneNumber;
            }
        }

        public double X()
        {

            return -5.7;
        }//X()

        public int Y(string id)
        {
            int g;
            g = Convert.ToInt32(id);
            return g + 100000;
        }//Y()

        public int Z() { int m = 100; return m; }

        public override string ToString()
        {            
            return lastName + "\t" + firstName + "\t" + id + "\t" + phoneNumber + "\t" + X() + "\t" + Y(id) + "\t Z is " + Z();
        }
    }
}


The problem is that you're adding items as strings to combobox instead of adding items itself:

Here:

this.comboBox1.Items.Add(aMember.FirstName.ToString());

Should be:

this.comboBox1.Items.Add(aMember);

In designer set ValueMember for combobox to be ID (msdn)

Instead of this (which doesn't make any sense):

foreach (var Item in comboBox1.Items);
    textBox2.Text += aMember.ID.ToString();

Use this:

textBox2.Text += combobox1.SelectedValue.ToString();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号