Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi There.....
I now develop app. that contains Design and database with SQL Server
and i have a combo Box that i want to fill it with a Field From my data base what i want now how can i do that ....?Please write down the code or give me any reference to learn that


thanks
Posted
Updated 3-Oct-10 20:35pm
v2

Here is an example ->

class Country
{
public Country(string value)
{
_name = value;
}

private string _name;

public string Name
{
get
{ return _name; }
set
{ _name = value; }
}
}

List < country > all = new List < country > ();
all.Add(new Country("X"));
all.Add(new Country("Y"));
all.Add(new Country("Z"));
//you can consider all is a list of data from your database table Country & Name is a column of the table.
comboBox1.DataSource = all;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";
comboBox1.SelectedIndex = 0;
 
Share this answer
 
v2
<br />
DataSet ds = new DataSet();<br />
<br />
            SqlConnection connection = new SqlConnection(@"Data Source=HEINZZ-PC\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True");<br />
            SqlDataAdapter da = new SqlDataAdapter();<br />
            da.SelectCommand = new SqlCommand("SELECT * FROM Personel", connection);<br />
            da.Fill(ds);<br />
<br />
            this.comboBox1.DataSource = ds.Tables[0];<br />
            this.comboBox1.DisplayMember = "Name";<br />
 
Share this answer
 
 
Share this answer
 
Use a search engine and search on c# databinding combobox.

You will get lots of hits, most of them will be about binding data to a combobox.

Read a few of them, try to implement what they cover and come back if you have a particular problem. Explain the problem and show the code causing the problem.

Good luck! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900