/// <summary> /// Gets the package id's from the DB /// </summary> private void GetBindedPackageIds() { //store the current selected index of combo box int selectedIndex = (cmbPackageIds.SelectedIndex < 0) ? 0 : cmbPackageIds.SelectedIndex; if (PackageIds.Count > 0) { PackageIds.Clear(); } //add to binding list foreach (Package package in PackageDB.GetAll()) { PackageIds.Add(package); } //set combo box back to selected index cmbPackageIds.SelectedIndex = selectedIndex; }
/// <summary> /// Gets the package id's from the DB /// </summary> private void GetBindedPackageIds(int defaultPackageID = 0) { int selectedIndex; try { List <Package> Packages = PackageDB.GetAll(); if (defaultPackageID >= 0) //store the current selected index of combo box { selectedIndex = Packages.FindIndex(x => x.PackageId == defaultPackageID); } else // select the first packageid in the index { selectedIndex = 0; } //prevent firing of events when a packageId is provided if (defaultPackageID > 0) { cmbPackageIds.SelectedIndexChanged -= cmbPackageIds_SelectedIndexChanged; } //set package ids datsource and index cmbPackageIds.DataSource = Packages; cmbPackageIds.SelectedIndex = selectedIndex; //resubscribe when a packageId is provided if (defaultPackageID > 0) { cmbPackageIds.SelectedIndexChanged += cmbPackageIds_SelectedIndexChanged; } } catch (Exception e) { MessageBox.Show(e.Message, "Unable to get package id's", MessageBoxButtons.OK, MessageBoxIcon.Error); } }