【问题标题】:combobox with for each loop每个循环的组合框
【发布时间】:2011-02-22 22:54:44
【问题描述】:

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

我在使用 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)
        {

        }
    }






}

【问题讨论】:

  • 你的循环没有意义。编写代码时,您应该了解您要完成的工作以及代码将如何完成它。
  • 你能修复代码的格式吗?部分代码未格式化。
  • 如果您的目标只是让组合框显示所选项目的文本,它会自动为您完成。您不需要 SelectedIndexChanged 处理程序。

标签: c# winforms datareader


【解决方案1】:

你只需要把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();
        }

【讨论】:

    【解决方案2】:

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

    下面的例子帮我找到了我需要的那个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2015-09-05
      • 2015-10-06
      相关资源
      最近更新 更多