2011-02-22 33 views
2

嗨,我是一個學生做任何人之後知道數據庫值填充組合框。如何在文本框中顯示相同的值。當我在組合框中選擇一個名稱時,應在我使用選擇項目的文本框中顯示相同的名稱。這是代碼。與每個循環的組合框

我使用foreach循環

foreach語句無法在類型「對象」的變量操作,因爲「對象」不包含「的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) 
     { 

     } 
    } 






} 
一個公共定義收到錯誤以下錯誤
+2

你的循環沒有意義的一個。當你編寫代碼時,你應該明白你想要完成什麼以及代碼如何完成它。 – SLaks 2011-02-22 22:56:42

+0

你能解決代碼的格式嗎?一些代碼沒有被格式化。 – RQDQ 2011-02-22 22:56:44

+0

http://stackoverflow.com/questions/5060300/combobox-examples – SLaks 2011-02-22 22:57:24

回答

4

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

以上的foreach(在comboBox1.Items VAR項目) 伊特m.ToString()僅返回系統類型

下面的例子爲我工作找到我需要

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