Click here to Skip to main content
16,023,124 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Sir,

I want to add items to combobox from database at loading time.

But it doesn't showing the values in the combobox, but in the textbox it is showing.
Is there any differnce in code to add to the combobox.

I wrote the code as follows: (values are retreiving by refering a datagrid selection changed)
C#
private void employeemasterdg_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    try
    {
        System.Collections.IList selectedUser = e.AddedItems;
        txtcode.Text = ((DataRowView)selectedUser[0]).Row.ItemArray[0].ToString();
        txtname.Text = ((DataRowView)selectedUser[0]).Row.ItemArray[1].ToString();
    }
    catch (Exception e1)
    {
        System.Windows.MessageBox.Show(e1.Message);
    }
    conn.Close();
    SqlDataReader rd3 = null;
    try
    {
        System.Collections.IList selectedUser = e.AddedItems;
        SqlCommand cmd3 = new SqlCommand();
        conn.Open();
        cmd3.Connection = conn;
        String str3 = "Select email,branch,department from empmaster where empcode=@ccode3";
        cmd3.Parameters.Add(new SqlParameter("ccode3", ((DataRowView)selectedUser[0]).Row.ItemArray[0].ToString()));
        cmd3.CommandText = str3;
        rd3 = cmd3.ExecuteReader();

        while (rd3.Read())
        {
            //HERE IS THE PROBLEM
            this.txtemail.Text = rd3.GetString(0);  //THIS DISPLAYS
            this.cmbtest.Text = rd3.GetString(1);  //THIS DOESN'T DISPLAY-THIS IS A COMBOBOX
            this.cmbdept.Text = rd3.GetString(2);  //THIS ALSO DOESN'T DISPLAY
            System.Windows.MessageBox.Show(rd3.GetString(1));  //THIS DISPLAYS
        }
    // ...
    }
    catch
    {
        // ...
    }
// ...
}

NB: It(the item which want to display) is in the combobox already. In WPF it is not showing a property SelectedText to the combobox, instead it is showing SelectedItem.
But I already had given as SelectedItem. Then also it didn't display.
Posted
Updated 12-Feb-10 23:25pm
v3

1 solution

LukmanulHakeem.T wrote:
this.cmbtest.Text = rd3.GetString(1);//THIS DOESN'T DISPLAY-THIS IS A COMBOBOX
this.cmbdept.Text = rd3.GetString(2);//THIS ALSO DOESN'T DISPLAY


Set the SelectedText instead, and make sure it's in the combobox already.
 
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