locked
DataBinding comboBox RRS feed

  • Question

  • When i try to bind my data to a ComboBox...instead of the data this shows up any reason why??? I want it to show the list of company names.... check out the code maybe someone can tell me where i am going wrong.....

    System.Data.DataViewManagerListItemTypeDescriptor

     


    string connect = System.Configuration.ConfigurationSettings.AppSettings["conn"];

    SqlConnection sql1 = new SqlConnection(connect);

    string str2 = "Select * From Customers";

    SqlCommand cmd1 = new SqlCommand(str2, sql1);

    sql1.Open();

    SqlDataAdapter da1 = new SqlDataAdapter(str2, sql1);

    DataSet ds1 = new DataSet("Customers");

    da1.Fill(ds1);

    comboBox1.DataSource = ds1;

    comboBox1.DisplayMember = "CompanyName";


     

    Monday, August 28, 2006 6:20 PM

Answers

  • Hi,

    set your DataSource to the first table in the dataset to make it work:

    comboBox1.DataSource = ds1.Tables[0];

    Andrej

    Monday, August 28, 2006 6:51 PM

All replies

  • Hi,

    set your DataSource to the first table in the dataset to make it work:

    comboBox1.DataSource = ds1.Tables[0];

    Andrej

    Monday, August 28, 2006 6:51 PM
  • I would recommend a slight change from Andrej's solution.

    comboBox1.DataSource = DS.Tables[0].DefaultView;
    comboBox1.DataBind();

    Hope it helps.

    Monday, August 28, 2006 6:58 PM
  • I would recommend a slight change from Andrej's solution.

    comboBox1.DataSource = DS.Tables[0].DefaultView;
    comboBox1.DataBind(); I get an error when I add this piece of code

     

    why is that considered better than the previous post????

    Monday, August 28, 2006 7:07 PM
  • Thread moved to Windows Forms Data Controls And DataBinding

    dataBind() is a method only available in ASP.NET

    the code should be:

    comboBox1.DataSource = ds1.Tables[0].DefaultView;

     

    this will automatically bind the values to the UI bindable component

    Tuesday, August 29, 2006 12:43 AM
  •  Tryin2Bgood wrote:
    comboBox1.DataSource = DS.Tables[0].DefaultView;

    why is that considered better than the previous post????

    You can't really bind to a datatable. By doing this, you really bind to the default view on that table (exposed by DefaultView property). When setting the datasource to ds1.Tables[0] you're actually setting it to ds1.Tables[0].DefaultView.

    Andrej

    Tuesday, August 29, 2006 6:00 AM