The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Parent child comboboxes

Last post 08-02-2007, 8:19 AM by Rich.Carpenter. 3 replies.
Sort Posts: Previous Next
  •  08-01-2007, 8:02 PM 4258

    Parent child comboboxes

    What is the best way to manage a parent/child relationship between two comboboxes on a form? Both comboboxes have collections as their datasources. When a selection is made in the parent combobox, I would like the child combobox item list to be limited to those items related to the item selected in the parent.

    I have tried the following, but the child combobox is never populated:

    Code:
            private void cboManufacturer_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (cboManufacturer.SelectedIndex != -1)
                {
                    vwAircraftModelGroupsCollection.Query.Where
                        (
                            vwAircraftModelGroupsCollection.Query.MftShortMake.Equal(cboManufacturer.SelectedValue)
                        );
                    vwAircraftModelGroupsCollection.Query.Load();
                    cboAircraftGroup.Refresh();
                }
            }
    

    The vwAircraftModelGroupsCollection (child) isn't loaded in this approach until an item is selected in the parent combobox (cboManufacturer). Is there a better way to do this?

  •  08-01-2007, 8:28 PM 4259 in reply to 4258

    Re: Parent child comboboxes

    The relation context for this sample snippet is Division -> Department. So Division is the parent and Department is the child.

     

    Code:
    /// <summary>
    /// Binds the divisions.
    /// </summary>
    private void BindDivisions()
    {
    	try
    	{
    		DivisionsCollection colDivisions = new DivisionsCollection();
    		colDivisions.Query.OrderBy(colDivisions.Query.Name.Ascending);
    
    		if (colDivisions.Query.Load())
    		{
    			this.cmbDivision.DataSource = null;
    			this.cmbDivision.DataSource = colDivisions;
    			this.cmbDivision.DisplayMember = DivisionsMetadata.PropertyNames.DisplayName;
    			this.cmbDivision.ValueMember = DivisionsMetadata.PropertyNames.DivisionId;
    		}
    	}
    
    	catch (Exception exc)
    	{
    		frmErrorDialog FriendlyError = new frmErrorDialog(exc);
    		FriendlyError.Show();
    	}
    }
    
    /// <summary>
    /// Handles the SelectedIndexChanged event of the cmbDivision control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    private void cmbDivision_SelectedIndexChanged(object sender, EventArgs e)
    {
    	try
    	{
    		if (this.cmbDivision.SelectedValue.ToString() != "BusinessObjects.Divisions")
    		{
    			BindDepartment(Convert.ToInt32(this.cmbDivision.SelectedValue));
    		}
    	}
    
    	catch (Exception exc)
    	{
    		frmErrorDialog FriendlyError = new frmErrorDialog(exc);
    		FriendlyError.Show();
    	}
    }
    
    /// <summary>
    /// Binds the department.
    /// </summary>
    /// <param name="divisionId">The division id.</param>
    private void BindDepartment(int divisionId)
    {
    	try
    	{
    		DepartmentsCollection colDepartments = new DepartmentsCollection();
    		colDepartments.Query.Where
    		(
    			colDepartments.Query.DivisionId.Equal(divisionId)
    		)
    		.OrderBy(colDepartments.Query.Name.Ascending);
    
    		if (colDepartments.Query.Load())
    		{
    			this.cmbDepartment.DataSource = null;
    			this.cmbDepartment.DataSource = colDepartments;
    			this.cmbDepartment.DisplayMember = DepartmentsMetadata.PropertyNames.DisplayName;
    			this.cmbDepartment.ValueMember = DepartmentsMetadata.PropertyNames.DepartmentId;
    		}
    	}
    
    	catch (Exception exc)
    	{
    		frmErrorDialog FriendlyError = new frmErrorDialog(exc);
    		FriendlyError.Show();
    	}
    }
     
    Regards,

    Scott Schecter
    EntitySpaces | My Site
  •  08-01-2007, 9:17 PM 4260 in reply to 4259

    Re: Parent child comboboxes

    The key is that the ComboBox items do not change until the DataSource changes. Calling Refresh only redraws the screen. Once you have loaded a new collection, you need to set the ComboBox DataSource to it.

    cboAircraftGroup.DataSource = vwAircraftModelGroupsCollection;


    David Neal Parsons
    www.entityspaces.net
  •  08-02-2007, 8:19 AM 4267 in reply to 4260

    Re: Parent child comboboxes

    Thanks, guys. You've answered both my questions: 1) it appears I was on the right track from a "best approach" standpoint, and 2) I now understand the problems I was having with the combobox control.

     

View as RSS news feed in XML