tags:

views:

585

answers:

1

How far is the code for best functionallity? I have two ComboBox, so the first is related for choose the company, and the second for choose the branch-office in relation with the one.

I note that the only way I can fill datasource with filtering .Where on LINQ is on this way, maybe Im wrong please take a moment for look the following snippet :

private void cboCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            var _index = ((ComboBox)sender).SelectedIndex;
            using (DB db = new DB())
            {
                var su = (from s in db.Branchs select s);

                if (cboCompany.SelectedIndex == 0)
                {
                    cboBranch.DataSource = su.Where(x => x.codeCompany == 1).Select(x => x.name).ToList();
                }
                else if (cboCompany.SelectedIndex == 1)
                {
                    cboBranch.DataSource = su.Where(x => x.codeCompany == 2).Select(x => x.name).ToList();
                }

                cboBranch.BindingContext = this.BindingContext;
                cboBranch.DisplayMember = "name";
                cboBranch.SelectedIndex = 0;
            }
        }

Thanks in Advance!

+2  A: 

Rather than hand-coding this, I would make data binding do all this work for me. In particular, it can be set up thus:

  • Make it so that your Company class has a property to get all associated branches - e.g. Company.Branches. If you use LINQ to SQL or Entity Framework, there should be one there already.
  • Have two BindingSources, bsCompanies and bsBranches.
  • Set cboCompany.DataSource to bsCompanies, and cboBranch.DataSource to bsBranches
  • Set bsCompanies.DataSource to collection/DataSet that contains companies.
  • Set bsBranches.DataSource to Branches under bsCompanies (the form designer should let you do this after you do the previous step, if your collection is strongly typed).

Now whenever user picks a different company in the first combo, the current item in the companies binding source will change. This will cause binding for the second binding source to re-evaluate, and set list of branches for a newly selected company to be the source for the second combo.

Pavel Minaev
Yep, that was on mind, by the way .. What happen if the Branch is an optional choose, I mean you need to check near the Branch Combo for enable this control and choose if you want, in this action the Binding Source fails or not?
Angel Escobedo
Why would it fail? It would still bing branches to companies, of course, it's just that user won't be able to interact with the branches combobox.
Pavel Minaev
I see, thanks for the info, Can you provide more links about advanced BindingSources like using generic or complex types
Angel Escobedo
I'm not sure what you mean. `BindingSource` doesn't really care whether the type is generic or not (it obviously cannot work with open generic types - i.e. `List<>` - but will happily process `List<Foo<Bar>>`) - so long as it can use reflection to get information about properties of said type. And I'm not sure what you mean by "complex type". In any case, the best reference is probably still MSDN: http://msdn.microsoft.com/en-us/library/ef2xyb33.aspx
Pavel Minaev