Dear Friends,
How do you bind data to a combo box ? I want to load data field like Customer Code in a combo box and when you are running the apllication, It should be appeared in a dropped down list. So hw do you do this?
Help me......
Dear Friends,
How do you bind data to a combo box ? I want to load data field like Customer Code in a combo box and when you are running the apllication, It should be appeared in a dropped down list. So hw do you do this?
Help me......
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Blocked AccountPosted May 14, 2008, 12:57 AM
That DataBind() method used in web application. I thought u want to bind the drop down list in the web application. but u want to bind the combo box in the window application. I am sending you the working code for that.
private void Form1_Load(object sender, EventArgs e)
{
ListCustomerCode();
}
public DataSet ActiveCust() // Create the data set
{
SqlConnection scon = new SqlConnection("server=.;database=Northwind;UID=sa;pwd=;");
scon.Open();
DataSet LCust = new DataSet();
string query = "SELECT Name,City FROM tblCity";
SqlCommand cmd = new SqlCommand(query, scon);
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(LCust, "LostCustomer");
scon.Close();
return LCust;
}
private void ListCustomerCode()
{
DataSet Lcust = new DataSet();
Lcust = ActiveCust();
if (!object.Equals(Lcust, null))
{
comboBox1.DataSource = Lcust.Tables[0];
comboBox1.DisplayMember = "Name";
}
}
Happy Coding!!Dinusha GamagePosted May 14, 2008, 12:01 AM
Thanks Dear, I have applied the coding you sent. Can you plz check this since there are some probs.
public
LostCustInfo ActiveCust() // Create the data set{
SqlConnection scon = new SqlConnection(con.SetConnection());scon.Open();
LostCustInfo LCust = new LostCustInfo(); string query = "SELECT cust_code,cust_name FROM CM_LOSTCUST WHERE active = 'Y'"; SqlCommand cmd = new SqlCommand(query, scon); SqlDataAdapter adpt = new SqlDataAdapter(cmd);adpt.Fill(LCust,
"LostCustomer");scon.Close();
return LCust;}
private void ListCustomerCode(){
LostCustInfo Lcust = new LostCustInfo();Lcust = Lcust.ActiveCust();
if (!object.Equals(Lcust, null)){
cboCustCode.DataSource = Lcust;
cboCustCode.DataBindings; // Here there is no method like this ????????
}
}
Dinusha GamagePosted May 13, 2008, 11:26 PM
Blocked AccountPosted May 13, 2008, 4:30 AM
Hi,
For binding the dropdownlist. write the method which returns all the customerid, as u want. then provide this datasource to the dropdownlist, in the page load or where u want. I am posting you the sample code for this.
dataSet =
new DataSet();dataSet = "Write the method which returnd the data."
if (!object.Equals(dataSet, null))
{
dropdownSample.DataSource = dataSet;
dropdownSample.DataBind();
}
Happy Coding!!