Windows Develop Bookmark and Share   
 index > Windows Forms General > populate a listbox on winform with values from database
 

populate a listbox on winform with values from database

Hi,

how to populate a listbox on winform with values from database at runtime? Can someone send code /links toarticles on netfor this..Once user selects clicksan item from this listbox, the selected item should be shown in a textbox. I have textbox in form1 and listbox in form 2.

Thanks to help.

yasinirshad  Tuesday, November 20, 2007 9:50 AM

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);

}

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

Yu Guo â€?MSFT  Monday, November 26, 2007 6:52 AM

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);

}

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

Yu Guo â€?MSFT  Monday, November 26, 2007 6:52 AM

You can use google to search for other answers

Custom Search

More Threads

• Displaying Good Quality Graphics in a large Simulation
• Fill the Combobox
• OpenFileDialog Redux
• DataGridView
• I have to resize a Control at runtime
• Loop Problem kinda
• Connect to Internet
• Using StreamWriter To Write HTML File
• Cannot assign ImageList in Beta 2
• tabcontrol scroll button