2014-12-27 56 views
1

我已經創建了一個代碼來顯示/刷新名爲patientTable的ListView。 數據庫:MS Access 2013 現在我想選擇一行並將檢索其上的指定列(例如,僅限patient_Id)並在標籤中顯示該選定值。我嘗試了一些我在互聯網上找到的代碼,但它仍然不起作用。如何從ListView中檢索指定值並將其綁定到標籤

public void refreshPatient() 
    { 
     try 
     { 
      patientTable.Items.Clear(); 
      con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database/Health.accdb;"); 
      con.Open(); 
      DataTable dt = new DataTable(); 
      OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Patient", con); 
      da.Fill(dt); 
      int i; 
      for (i = 0; i <= dt.Rows.Count - 1; i++) 
      { 
       patientTable.Items.Add(dt.Rows[i].ItemArray[0].ToString()); 
       String strName = ""+dt.Rows[i].ItemArray[1].ToString()+", "+dt.Rows[i].ItemArray[2].ToString()+" "+dt.Rows[i].ItemArray[3].ToString(); 
       patientTable.Items[i].SubItems.Add(strName); 
       patientTable.Items[i].SubItems.Add(dt.Rows[i].ItemArray[6].ToString()); 
      } 
      con.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(""+ex); 
     } 
    } 
+0

加載標籤的代碼在哪裏? – andy

回答

0
int patiendID = patientTable.Items[i]["patient_Id"] 

lblSomeLabel.Text = patiendID; //or patientTable.Items[i]['patient_Id']; 
0

什麼,我從你的問題的理解,你想創建一個自動完成像谷歌網站確實,是嗎?在這種情況下,我寫了示例代碼,此代碼的工作方式類似於auto complete,它會自動填充存儲在數據庫中的剩餘方框。

Manager類:

private static string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database/Health.accdb"; 

public static void AutoComplete(AutoCompleteStringCollection _collections) 
     { 
      using (OleDbConnection connection = new OleDbConnection(connectionString)) 
      { 
       string query = "SELECT DISTINCT [Description] FROM [Database] ORDER BY [Description] ASC"; 

       connection.Open(); 

       using (OleDbCommand command = new OleDbCommand(query, connection)) 
       using (OleDbDataReader reader = command.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         _collections.Add(reader["Description"].ToString()); 
        } 

        reader.Close(); 
       } 

       connection.Close(); 

      } 
     } 

public static void GetData(TextBox _windowsTextBox1, TextBox _windowsTextBox2, TextBox _windowsTextBox3, NumericUpDown _numericUpDown1) 
     { 
      using (OleDbConnection connection = new OleDbConnection(connectionString)) 
      { 
       string query = "SELECT [ProductCode], [Description], [Quantity], [Price] FROM [Database] WHERE [Description] = @Description"; 

       connection.Open(); 

       using (OleDbCommand command = new OleDbCommand(query, connection)) 
       { 
        command.Parameters.Add("@Description", OleDbType.VarChar); 
        command.Parameters["@Description"].Value = _windowsTextBox1.Text; 

        using (OleDbDataReader reader = command.ExecuteReader()) 
        { 
         while (reader.Read()) 
         { 
          string productCode = (string)reader["ProductCode"]; 
          string description = (string)reader["Description"]; 

          UserInformation.Description = description; 

          int quantity = (int)reader["Quantity"]; 
          int price = (int)reader["Price"]; 

          _windowsTextBox2.Text = productCode; 

          _windowsTextBox3.Text = Convert.ToString(price); 

          _numericUpDown1.Maximum = Convert.ToDecimal(quantity); 
         } 

         reader.Close(); 
        } 
       } 

       connection.Close(); 

      } 
     } 

實例形式:

private void AutoComplete() 
     { 
      AutoCompleteStringCollection _dataCollections = new AutoCompleteStringCollection(); 

      Manager.AutoComplete(_dataCollections); 

      textBox2.AutoCompleteMode = AutoCompleteMode.Suggest; 

      textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource; 

      textBox2.AutoCompleteCustomSource = _dataCollections; 
     } 

private void textBox2_TextChanged(object sender, EventArgs e) 
     { 
      Manager.GetData(this.textBox2, this.textBox1, this.textBox3, this.numericUpDown1); 
     } 

這裏是結果:

Result

五月這個答案會幫助你!

乾杯!

相關問題