如何从ListView中检索指定的值并将其绑定到标签

时间:2014-12-27 05:56:31

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

我已经制作了一个代码来显示/刷新名为patientTable的ListView。 数据库:MS Access 2013 现在我想选择一行,并在其上检索指定的列(例如,仅限patient_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);
        }
    }

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我从您的问题中了解到,您想创建一个像谷歌网站一样的自动完成,是吗?在这种情况下,我已经编写了示例代码,这段代码就像自动完成一样,它会自动填充存储在数据库中的剩余框。

经理级别:

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

愿这个答案对你有帮助!

干杯!