2015-02-06 120 views
0

在我的winform中,有一個包含2 column, 1st one is studentname & 2nd column is StudentAverage的數據表。
首先txtbx是autosuggest(studentname)。 如何使第二txtbx根據第一txtbx automaticaly填充(第二txtbx填充第二柱(studentsavrage)的相應記錄如何根據第一個文本框自動填充第二個文本框以及數據庫中的相應數據

這是針對第一txtbx;顯示StudentNames

private void txtbxName_TextChanged(object sender, EventArgs e) 
    { 

      AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection(); 
      string StrCmd = "SELECT * FROM School"; 
      string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\School_Database.accdb"; 
      OleDbConnection MyConn = new OleDbConnection(ConnStr); 
      MyConn.Open(); 
      OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn); ; 
      OleDbDataReader ObjReader = Cmd.ExecuteReader(); 
      if (ObjReader != null) 
     { 
      while (ObjReader.Read()) 
       namesCollection.Add(ObjReader["StudentName"].ToString()); 
     } 
     else 
     { 
      MessageBox.Show("Data not found"); 
     } 
      ObjReader.Close(); 
      MyConn.Close(); 
      txtbxName.AutoCompleteMode = AutoCompleteMode.Suggest; 
      txtbxName.AutoCompleteSource = AutoCompleteSource.CustomSource; 
      txtbxName.AutoCompleteCustomSource = namesCollection;   
    } 

回答

0

我假設你正在用WinForms開發

首先:由於你的查詢中缺少where子句,所以你在用戶輸入文本時沒有進行過濾,你應該在另一個地方移動AutoCompleteStrinCollection獲取if你不想那樣。

您的問題:這裏最簡單的方法是手動更新您的txtbxName_TextChanged方法中的第二個文本框。

private void txtbxName_TextChanged(object sender, EventArgs e) 
{ 
    string StrCmd = String.Format("SELECT AGE FROM School WHERE name '{0', txtbox1.Text"); 
    //perform query as you already do and read result 
    textbox2.Text = cmd.ExecuteScalar() 
} 

P.S:在現實世界中的事情都沒有做,你是在很多很多方面

+0

謝謝您的回答做的方式,但它沒有工作! – beginnner 2015-02-16 17:22:22

相關問題