2014-12-27 11 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

五月这个答案会帮助你!

干杯!