Bob
Bob

Reputation: 1375

How to populate client combobox with items from service's list of objects

EDIT: http://pastebin.com/EVjD95RY

snippet of service's code

My Windows Forms WCF client has two combo boxes - combobox1 and combobox2.

My Web Based WCF service has a List of objects with information:

public List<CompanyInfo> companies = new List<CompanyInfo>();

Don't know why companies isn't available to me in client. I made the following reference to the service inside client...

Client.ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();

...which should have allowed me to access comapnies like so: client.companies

A typical object of CompanyInfo has the following data members:

companyName, address, type

Inside my client I made it so when I select an item from combobox1, let's say Technology, the combobox2 will become enabled and should populate itself with all of the objects from companies that have their type data member set to 'technology'.

Problem is I can't figure it out. I am trying to use foreach like so:

    private void combobox1_SelectedValueChanged(object sender, EventArgs e)
            {
                if (combobox1.Text != "")
                {
                    combobox2.Enabled = true;
// For every object inside the companies list that have their type set to technology put them into the combobox, disregards the other types
                    if (combobox1.SelectedText.Equals("Technology"))
                    {

                        foreach(ServiceReference1.CompanyInfo ci in ?)
                        {
                            if(ci.Type.Equals("technology"))
                                combobox2.Items.Add(?);
                        }
                    } // more options i.e. combobox1.SelectedText.Equals("Medicine")
                }
                else
                    combobox2.Enabled = false;
            }
        }

Upvotes: 0

Views: 1305

Answers (1)

GuyVdN
GuyVdN

Reputation: 692

On your webservice you could create a method GetCompaniesOfType and pass in the selected value. Preferably some kind of ID and not a string value. With the result of that method you can populate your second combobox. If the list of companies does not change a lot you can add client and/or server side caching to load the data faster.

Code would become something like this

Private void combobox1_SelectedValueChanged(object sender, EventArgs e)
{
    combobox2.Items.Clear();

    if (combobox1.SelectedValue == null)
        return;

    var companies = ServiceReference1.GetCompaniesOfType(combobox1.SelectedValue);
    combobox2.Items.Add(companies);
}

Upvotes: 1

Related Questions