【问题标题】:How to retrieve a specified value from ListView and bind it to a label如何从 ListView 中检索指定的值并将其绑定到标签
【发布时间】:2014-12-27 05:56:30
【问题描述】:

我编写了一个代码来显示/刷新名为 PatientTable 的 ListView。 数据库:MS Access 2013 现在我想选择一行并将检索其上的指定列(例如仅患者 ID)并在标签中显示该选定值。我尝试了一些在互联网上找到的代码,但仍然无法正常工作。

    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);
        }
    }

【问题讨论】:

  • 加载标签的代码在哪里?

标签: c# winforms listview ms-access-2013


【解决方案1】:
 int patiendID = patientTable.Items[i]["patient_Id"]

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

【讨论】:

    【解决方案2】:

    我从你的问题中了解到,你想像谷歌网站一样创建一个自动完成,是吗?在这种情况下,我编写了示例代码,该代码的工作方式与自动完成类似,它会自动填充存储在数据库中的剩余框。

    经理类:

    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);
            }
    

    结果如下:

    希望这个答案对你有帮助!

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-07
      • 2018-10-24
      • 2020-02-10
      • 2017-08-24
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多