Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I am trying to simply populate a ComboBox with data from a Sql server table column (adhering to the MVVM design pattern), and do not understand why it is not working. I am binding it to a BindableCollection (Stylet framework), which is basically an ObservableCollection.
There is automatic coupling between the View and the ViewModel (Stylet does this).

This is my method:

C#
public void FillComboBoxLicenseHolders() {

    try {

        DataSet ds = new DataSet();

        using (SqlConnection sqlCon = new SqlConnection(ConnectionString.connectionString)) {

            SqlDataAdapter sqlDA = new SqlDataAdapter();
            sqlDA.SelectCommand = new SqlCommand("select Foretaksnavn from tblLicenseHolder", sqlCon);
            sqlDA.Fill(ds);
        }

        DataTable dt = new DataTable();
        dt = ds.Tables[0];

        for (int i = 0; i < dt.Rows.Count; i++) {

            DataRow dr = dt.NewRow();
            dr = dt.Rows[i];
            LicenseHolder licenseHolder = new LicenseHolder();
            licenseHolder.Foretaksnavn = dr["Foretaksnavn"].ToString();

            LicenseHolders.Add(licenseHolder);
        }

    } catch (Exception ex) {

        MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK, MessageBoxImage.Information);
    }
}


This is how I instantiate my BindableCollection:

private BindableCollection<LicenseHolder> _licenseHolders;
public BindableCollection<LicenseHolder> LicenseHolders {

    get => _licenseHolders;
    set => SetAndNotify(ref this._licenseHolders, value);
}

public TripViewModel(IWindowManager windowManager) {

    this.windowManager = windowManager;
    LicenseHolders = new BindableCollection<LicenseHolder>();
}


This is my ComboBox in the XAML:

<ComboBox Name="cbLicenseHolder" Canvas.Left="50" Canvas.Top="204" Width="291"
          ItemsSource="{Binding LicenseHolders, Mode=TwoWay}"
          Loaded="{s:Action FillComboBoxLicenseHolders}"/>


What I have tried:

I've spent the better part of a day trying to Google my way through this..

When I run my app, there's no error message, but the ComboBox displays no results.
If I debug and put a break point, I can see that it does indeed read the sql server column data, but it is still not showing.

Any ideas?
Posted
Updated 11-Oct-22 8:45am
Comments
Chris Copeland 5-Aug-21 10:18am    
I can't immediately see a problem with this code, how are you binding the ViewModel to the Window itself? And are you using the ComboBox within a template or style? Have you confirmed that the query is loading values from the database, ie. check with a debugger to step through the code?

I will note that the ComboBox isn't provided with a DisplayMemberPath so it will probably try to call ToString() on each of the objects in the collection.
olegul86 5-Aug-21 11:52am    
Hi @Chris Copeland, and thanks for your response. The framework takes care of the coupling between the ViewModel and the View, which has worked nicely on other parts of my app. I do not think I am using the ComboBox within a template or a style, and I'm not sure completely sure what that means. I did step through the code, and the for loop in my method is able to count all 8 rows in my sql table column, and when I get to "LicenseHolders.Add(licenseHolder); it does in fact hold the first row.
[no name] 5-Aug-21 17:42pm    
You did not specify a "member" to display, so the combobox is going to use thatever is returned by your "combo item's" ToString() method. Assuming the Combox actually has items, isn't collapsed / not visible; which you didn't confirm using debug.
olegul86 6-Aug-21 5:49am    
Thank you very much!

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