Dear Friends,
I want to bind data to a combo box and display it in a drop down list when it runs the application.
In the given below coding I have done coding but there are some problems. Here I have created the dataset and then load the data from data source. After that, I'm trying to bind data to the combo box and display that in the drop down list..... So, that is difficult for me..Hope anybody can help me.........
Thanks in advence.
public
LostCustInfo ActiveCust(){
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.DataBind(); // there is no method like this ?????
}
}
Neetika GuptaPosted Jun 9, 2008, 7:11 AM
"SELECT cust_code, " + ' ' + "cust_name AS Name FROM customers";
You can use the same code:
if (!object.Equals(Lcust, null))
{
comboBox1.DataSource = Lcust.Tables[0];
comboBox1.DisplayMember = "Name";
}
Neetika GuptaPosted Jun 9, 2008, 7:08 AM
"SELECT cust_code, " + ' ' + "cust_name FROM customers";
This query will fetch cust_code and cust_name as one column with space in between.
Dinusha GamagePosted May 14, 2008, 2:47 AM
Thanks Manish....
I need to know that If you want to display two fields in the drop down list, How do you do that ? (such as cust_code and cust_name) Could you plz notify that in the given below coding?
if (!object.Equals(Lcust, null))
{
comboBox1.DataSource = Lcust.Tables[0];
comboBox1.DisplayMember = "Name";
}
Blocked AccountPosted May 14, 2008, 1:07 AM
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!!