简体   繁体   English

C# - 如何找到列表框项的数据库键?

[英]C# - How do I find the database key of a Listbox item?

I am writing an application where the surname and first name of an employee is shown in a (sorted) listbox - for a selected business unit (hard coded in example below) using an sql statement.我正在编写一个应用程序,其中员工的姓氏和名字显示在(排序的)列表框中 - 对于使用 sql 语句的选定业务单位(在下面的示例中进行了硬编码)。 The index of the employee (Access) database is the employee number.雇员(Access)数据库的索引是雇员编号。

When the item is selected from the listbox I can't find a way to determine the employee number that enables me to read the database to obtain attributes about the employee on a new form.当从列表框中选择项目时,我找不到确定员工编号的方法,该编号使我能够读取数据库以在新表单上获取有关员工的属性。 I think the trick maybe using keyvaluepair but need some guidance on how this is coded.我认为这个技巧可能使用 keyvaluepair 但需要一些关于如何编码的指导。 A sample of the code is below and thanks in anticipation of your help.代码示例如下,感谢您的帮助。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private OleDbConnection connection = new OleDbConnection();
        public Form1()
        {
            InitializeComponent();
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Mark\Documents\Employee.accdb;Persist Security Info=False;";
            listBox1_Load();
            }

    void listBox1_Load()
        {
            try
            {
            // Open database and select the data to be shown in the List Box - hard-coded example here
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select ID, FirstName, Surname from Employee where BusinessUnit = 'Finance'";
            command.CommandText = query;

            OleDbDataReader reader = command.ExecuteReader();

            // Read records returned by the query and populate the list box with the employee name
            while (reader.Read())
            {
                listBox1.Items.Add(reader["Surname"] + ", " + reader["FirstName"]);
            }
            connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

I would store the data from the DB in a Dictionary, let's say我会将数据库中的数据存储在字典中,比方说

Dictionary<int,String[]>

where the key is ID and the array is composed of Name and Surname.其中键是 ID,数组由 Name 和 Surname 组成。 Then you populate the listBox with the Dictionary data.然后用字典数据填充列表框。

Not an appropriate answer but an idea:不是一个合适的答案,而是一个想法:

Maybe you could try something like converting the result into an array or dictionary也许您可以尝试将结果转换为数组或字典

listBox1.DataSource = <Array or Dictionary>;
listBox1.DisplayMember = "NameOfColumnToBeDisplayed";
listBox1.ValueMember = "NameOfColumnToUseValueFrom";

And the Display != Value (eg value would be your employee id)和 Display != Value(例如,值将是您的员工 ID)

not sure if you can add a hidden field into the listbox.不确定是否可以在列表框中添加隐藏字段。 however if you store the query you received into a variable which is retained (maybe a datatable as opposed to using a datareader), then when a user clicks to select an option from the listbox you can use that to find the associated record in the query using the index of the listbox selected item.但是,如果您将收到的查询存储到保留的变量中(可能是数据表,而不是使用数据读取器),那么当用户单击以从列表框中选择一个选项时,您可以使用它来查找查询中的关联记录使用列表框所选项目的索引。

however you must ensure that the query does the sorting of the results and not the listbox.但是,您必须确保查询对结果进行排序,而不是对列表框进行排序。

Mike麦克风

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c#winforms应用程序中获取列表框项目的“密钥”? - How do I get at the listbox item's “key” in c# winforms app? 如何获取列表框中所选项目的编号(C#) - How do I get the number of the item selected in a listbox (C#) 如何单击一个按钮,该按钮将从ListBox中删除一个项目,而该列表中包含C#中ListBox的信息? - How do I click a button that will remove an item from a ListBox and the List that holds the information for the ListBox in C#? C#更新数据库中listBox中的每个项目 - C# Update Database for every Item in listBox C#数据库列表框项删除 - c# database listbox item remove Visual C#:如何将click事件附加到列表框中的项目? - Visual C#: How do I attach a click event to an item in listbox? C#如何使用代码检查列表框中的选定项目? - C# How do I check a selected item in a listbox using code? C#如何为列表框中的选定项目运行一些代码? - C# How do I run some code for the selected item in a listbox? c#如何获取列表框中的选定项目是否已更改。 ? - c# how do i get check if a selected item in a listbox has changed. ? 如何从ListBox中获取随机项,然后在C#中进行比较? - How do I get a random item from a ListBox, then compare it in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM