Click here to Skip to main content
15,887,450 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
When I run the program, the combo box only shows:
"System.Data.DataRowView"


C#
private void GetSupplierName()
        {
            string query = "Select * from Supplier";
            SqlCommand cmd = new SqlCommand(query, cn);
            cmd.CommandText = query;
            //cn.Open();

            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            adpt.Fill(ds);
            cbosupplier.Items.Clear();
            cbosupplier.DisplayMember = "SupplierName";
            cbosupplier.ValueMember = "SupplierNo";
            cbosupplier.DataSource = ds.Tables[0];  
        }
Posted
Updated 23-Jun-12 21:51pm
v2

C#
private void GetSupplierName()

        {
         try
            {
                cbosupplier.Items.Clear();

                string query = "Select distinct SupplierName from Supplier" +
                    " order by SupplierName asc";
                SqlCommand cmd = new SqlCommand(query, cn);
                cmd.CommandText = query;
                

                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    cbosupplier.Items.Add(dr["SupplierName"].ToString());
                }
            }
            catch
            {
                MessageBox.Show("Error in Connection");
            }
           }
 
Share this answer
 
Using DisplayMemberPath wasn't working but rather ItemTemplate. Here is an example.

XML
<Window.Resources>
  <DataTemplate x:Key="comboTemplate">
        <TextBlock Text="{Binding Path=username}" />
  </DataTemplate>
</Window.Resources>
<ComboBox Margin="18,121,24,0" Name="cmbEmail" Tag="email" TabIndex="1" ToolTip="enter the email you signed up with here" IsEditable="True" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource comboTemplate}"  ItemsSource="{Binding}" Height="23" VerticalAlignment="Top" Style="{DynamicResource cmbBoxerrors}">
            <ComboBox.Text>
                <Binding Path="username"/>
            </ComboBox.Text>
 </ComboBox>
 
Share this answer
 
 
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