Below is the code which might help you. I am using MySQLDatabase, but the code shoudl work regardless of what DBServer we are using. So replace all my MySQL operations with what you're using as usual. After you get data in datatable, try to follow the scenario
as below
Assumming ComboBox 1 enlists all the colums ( "headers" ) of the database and ComboBox 2 needs to populate all the records of that column :
On from load
foreach (DataColumn c in dtCustomers.Columns)
{
comboBox1.Items.Add(c.ColumnName);
}
and on the comboBox1_SelectedIndexChanged event ...
comboBox2.Items.Clear();
foreach (DataRow dr in dt.Rows)
{
comboBox2.Items.Add(dr[comboBox1.SelectedItem.ToString()].ToString());
}
I've created a dedicated sample Windows forms application for this example and uploaded to my code library. You are free to download it from
http://sdrv.ms/S5KzC5 | File name : ComboBoxMySQLDatabaseConnectivityDemo.zip
Please mark this post as an answer if it solves your problem - Thanks much.
roani123
0 Points
3 Posts
using column header as data display member
Nov 20, 2012 04:53 AM|LINK
Good day,
I have a question about combo box. How can I use the column header of my database for the selection in combo box.
I am using c# and sql for database
,Thanks
Usha82
Member
710 Points
129 Posts
Re: using column header as data display member
Nov 20, 2012 04:59 AM|LINK
You can create a stored procedure and pass table name as a parameter to it. Use the parameter instead of hardcoded value 'tab1'.
aarsh
Participant
1543 Points
426 Posts
Re: using column header as data display member
Nov 25, 2012 10:05 PM|LINK
Below is the code which might help you. I am using MySQLDatabase, but the code shoudl work regardless of what DBServer we are using. So replace all my MySQL operations with what you're using as usual. After you get data in datatable, try to follow the scenario as below
Assumming ComboBox 1 enlists all the colums ( "headers" ) of the database and ComboBox 2 needs to populate all the records of that column :
On from load
foreach (DataColumn c in dtCustomers.Columns) { comboBox1.Items.Add(c.ColumnName); }and on the comboBox1_SelectedIndexChanged event ...
comboBox2.Items.Clear(); foreach (DataRow dr in dt.Rows) { comboBox2.Items.Add(dr[comboBox1.SelectedItem.ToString()].ToString()); }I've created a dedicated sample Windows forms application for this example and uploaded to my code library. You are free to download it from http://sdrv.ms/S5KzC5 | File name : ComboBoxMySQLDatabaseConnectivityDemo.zip
Please mark this post as an answer if it solves your problem - Thanks much.