惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
V
V2EX
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
I
InfoQ
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
H
Help Net Security
腾讯CDC
D
Docker
P
Proofpoint News Feed
GbyAI
GbyAI
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏

博客园 - lining

输出的客户端 - lining - 博客园 List<T> - lining - 博客园 DateTime 赋空值 配色达人速成 批处理文件,删除文件及文件夹 - lining - 博客园 Source Code中pfx的密码 查找所有有某一列的表 如何修改 app.config 的配置信息 用例的获得 如何oracle调试存储过程 C# 中String 和 string 有什么区别 ora-00997:非法使用LONG数据类型 oracle如何判断一个字符串是否为数字或日期 C# 中的计时器 winform技巧,listbox绑定,value,text, - lining - 博客园 查看oracle执行计划 “Windows Workflow Foundation开发实战系列课程”中的数据库恢复 ASP.NET多语言版的开发 oracle数据导入导出imp/exp命令
populate a listbox on winform with values from database
lining · 2010-06-18 · via 博客园 - lining

from->http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9dcdf5a2-d3df-4d27-8352-1cfa9997b685

Hi,

how to populate a listbox on winform with values from database at runtime?  Can someone send code / links to articles on net for this..Once user selects clicks an item from this listbox, the selected item should be shown in a textbox. I have textbox in form 1 and listbox in form 2.

Thanks to help.

=================================================================

Hi, yasinirshad,

Based on my understanding, you want to know how to get the values from a DataBase and add them into a ListBox, don't you?

I wrote a small sample for you which can do this job.

Say, I am using a Acess DB and two forms.

In form1.

Code Block

DataTable table;

BindingSource source;

private void Form1_Load(object sender, EventArgs e)

{

OleDbConnection connection = new OleDbConnection(Properties.Settings.Default.newConnectionString);

OleDbDataAdapter adapter = new OleDbDataAdapter("select NAME, ID from Table1", connection);

table = new DataTable();

adapter.Fill(table); //Retrieve data from DataBase

source = new BindingSource();

source.DataSource = table;

this.textBox1.DataBindings.Add("Text", source, "Name"); //Bind the DataSource to the TextBox

Form2 form = new Form2(source);

form.Owner = this;

form.Show(this);

}

In form2

Code Block

public Form2(BindingSource source)

{

InitializeComponent();

listBox1.DisplayMember = "NAME";

listBox1.ValueMember = "ID";

listBox1.DataSource = source;

}

And the value in your TextBox will change with the selection in your ListBox.

More info

http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.80).aspx

http://msdn2.microsoft.com/en-us/library/ef2xyb33.aspx

Hope this helps,

Regards