带有 for 每个循环的组合框

发布于 2024-10-18 13:40:20 字数 3736 浏览 5 评论 0 原文

您好,我是一名学生,在用数据库值填充组合框后有人知道吗?如何在文本框中显示相同的值。当我在组合框中选择一个名称时,我正在使用所选项目的文本框中应显示相同的名称。这是代码。

我收到以下错误:使用 foreach 循环

foreach 语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义

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.SelectedItem)
                textBox2.Text += item.ToString();
        }


        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }






}

Hi I am a student do anybody know after populating the combobox with the database values. How to display the same value in the text box. When I select a name in the combobox the same name should be displayed in the text box I am using seleted item. Here is the code.

I am getting the error the following error using the foreach loop

foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

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.SelectedItem)
                textBox2.Text += item.ToString();
        }


        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }






}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2024-10-25 13:40:20

您只需将 foreach 循环更改为:

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 += item.ToString();
        }

You just need to change the foreach loop to:

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 += item.ToString();
        }
相思碎 2024-10-25 13:40:20

上面的 foreach(comboBox1.Items 中的 var item)
item.ToString() 仅返回系统类型

下面的示例帮助我找到了我需要的类型

foreach(DataRowView item in cmbUser.Items)
{
    if(item.Row[0].ToString() == "something")\\gets value displayed
    {
        //do something
    }
}

the above foreach(var item in comboBox1.Items)
item.ToString() just returns the system type

the below example worked for me to find the one I needed

foreach(DataRowView item in cmbUser.Items)
{
    if(item.Row[0].ToString() == "something")\\gets value displayed
    {
        //do something
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文