Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have two combobox. First get populated on page load event and second get populated on selectedindexchange event of first combobox.
Here is my code:
C#
dl is object of DAL class

 private void Frm_Incentives_Load(object sender, EventArgs e)
        {
            
            dl.Fn_Select(cmbCostCenter);
        }

private void cmbCostCenter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cmbCostCenter.Items.Count>0)
            {
                dl.Fn_Select_WithParametre(cmbCostCenter.SelectedValue.ToString(),cmbEmployee);
            }
        }

===============DAL========

internal void Fn_Select(ComboBox combo)
        {
            try
            {
              
                con.Open();
                cmd = new SqlCommand("select CCN,CCn_Name from Dept ", con);
                adpt = new SqlDataAdapter(cmd);
                adpt.Fill(ds);
                combo.Items.Clear();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    combo.DataSource = ds.Tables[0];//Error
                    combo.DisplayMember = ds.Tables[0].Columns[1].ToString();
                    combo.ValueMember = ds.Tables[0].Columns[0].ToString();
                }
                con.Close();
               

            }
            catch(Exception ex)
            {
               
                con.Close();
            }
        }

 internal void Fn_Select_WithParametre(string code,ComboBox combo)
        {
            try
            {
               
                con.Open();
                cmd = new SqlCommand("select Emp_Code,Emp_Name from Master where CCN="+code+" ", con);
                adpt = new SqlDataAdapter(cmd);
                adpt.Fill(ds);
                combo.Items.Clear();
                if(ds.Tables[0].Rows.Count>0)
                {
                combo.DataSource = ds.Tables[0];
                combo.DisplayMember = ds.Tables[0].Columns[3].ToString();
                combo.ValueMember = ds.Tables[0].Columns[4].ToString();
                con.Close();
                }               
            }
            catch (Exception ex)
            {
              
                con.Close();
            }
        }
Posted
Comments
sudipta biswas 19-Jun-13 6:21am    
What is the problem?
Jignesh Khant 19-Jun-13 8:02am    
You can use Drop Down List Selected Index Change property like this:
Protected Sub ddlCompany_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Member 8233601 20-Jun-13 0:56am    
problem is onload of form combo1.selectedindexchanged get fired after combo1.datasource=ds.table[0]

1 solution

Instead of using the SelectedIndexChanged event try using SelectionChangeCommitted
 
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