private void LoadListBox()
{
this.listBox1.DataSource = null;
this.listBox1.Items.Clear();
connString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source= D:\VS2005\練習" +
@"\TestDB.mdb";
selectCmd = "SELECT * from dep";
//1.建立連線
using (OleDbConnection cn = new OleDbConnection(connString))
{
//2.開啟連線
cn.Open();
//3.建立OleDbCommand物件
using (OleDbCommand cmd = new OleDbCommand(selectCmd, cn))
{
//4.建立OleDbDataReader物件
using (OleDbDataReader dr = cmd.ExecuteReader())
{
//5.建立DataTable
DataTable dt = new DataTable();
//6.載入OleDbDataReader
dt.Load(dr);
//7.設定DataSource
listBox1.DataSource = dt;
//8.顯示成員
listBox1.DisplayMember = "DepNo";
}
}
}
資料來源~http://www.dotblogs.com.tw/yc421206/archive/2009/07/12/9356.aspx
使用DoubleClick事件將ListBox內Item帶入
private void listBox1_DoubleClick(object sender, EventArgs e)
{
string curItem = listBox1.Text.ToString();
tbxDepno.Text = curItem;
}
沒有留言:
張貼留言