private void SelectCategoriesButton_Click(object sender, EventArgs e) { var categories = CategoryCheckedListBox.CheckedCategoriesList().Select(category => (int?)category.CategoryID); var ops = new DataOperations(); var data = ops.ProductsByCategories(categories.ToList()).ToList(); ProductDataGridView.DataSource = data; ProductDataGridView.ExpandColumns(); }
/// <summary> /// Read all categories without the change tracker /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void MainForm_Shown(object sender, EventArgs e) { using (var context = new NorthWindContext()) { await Task.Run(async() => { var categories = await context.Categories.AsNoTracking().ToListAsync(); CategoryCheckedListBox.InvokeIfRequired(clb => clb.DataSource = categories); }); } GetSelectedButton.Enabled = true; }
private async void GetSelectedButton_Click(object sender, EventArgs e) { ResultsTextBox.Text = ""; var sb = new StringBuilder(); /* * Get all checked categories primary key in the checked list box */ var indices = CategoryCheckedListBox.SelectedCategoryIdentifier(); if (indices.Length <= 0) { return; } using (var context = new NorthWindContext()) { /* * Get all products for each category, not FindAllAsync expects an * object array not an int array so they are converted via * Array.ConvertAll. */ Categories[] categories = await context.FindAllAsync <Categories>(Array.ConvertAll(indices, id => (object)id)); /* * Display in a text box */ foreach (Categories category in categories) { sb.AppendLine(category.CategoryName); List <Products> products = context.Products .AsNoTracking().Where(prod => prod.CategoryId == category.CategoryId) .ToList(); foreach (Products product in products) { sb.AppendLine($" {product.ProductId,-4}{product.ProductName}"); } sb.AppendLine(); } ResultsTextBox.Text = sb.ToString(); } }
/// <summary> /// Populates input fields with data of the selected item in the list view. /// </summary> private void RecipesListView_SelectedIndexChanged(object sender, EventArgs e) { if (RecipesListView.SelectedItems.Count != 0) { // get the selected item var selectedRecipe = RecipesListView.SelectedItems[0]; // multiselect is disabled. there is only one selected item // populate inputs RecipeTitleTextBox.Text = selectedRecipe.SubItems[0].Text; TimeNumericUpDown.Value = int.Parse(selectedRecipe.SubItems[1].Text); DifficultyComboBox.SelectedItem = selectedRecipe.SubItems[2].Text; CategoryCheckedListBox.ClearSelected(); string[] selectedCategories = selectedRecipe.SubItems[3].Text.Split(new string[] { ", " }, StringSplitOptions.None); foreach (string selectedCategory in selectedCategories) { int choiceIndex = CategoryCheckedListBox.Items.IndexOf(selectedCategory); if (choiceIndex != -1) { CategoryCheckedListBox.SetItemChecked(choiceIndex, true); } } } }