従業員の姓と名が(ソートされた)リストボックスに表示されるアプリケーションを作成しています-選択したビジネスユニット(以下の例ではハードコードされています)について、SQLステートメントを使用しています。従業員(アクセス)データベースのインデックスは、従業員番号です。
リストボックスからアイテムを選択すると、データベースを読み取って新しいフォームで従業員に関する属性を取得できる従業員番号を判別する方法が見つかりません。トリックはおそらくkeyvaluepairを使用していると思いますが、これをどのようにコーディングするかについてのガイダンスが必要です。コードのサンプルを以下に示します。あなたの助けを期待して感謝します。
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)
{
}
}
}
リストボックスに非表示フィールドを追加できるかどうかわからない。ただし、受け取ったクエリを保持される変数(おそらく、データリーダーを使用するのではなくデータテーブル)に格納する場合、ユーザーがリストボックスからオプションをクリックして選択すると、それを使用してクエリ内の関連レコードを見つけることができます。リストボックスで選択したアイテムのインデックスを使用します。
ただし、クエリがリストボックスではなく結果の並べ替えを行うようにする必要があります。
マイク
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加