private void AcceptFilter_Click(object sender, RoutedEventArgs e) { int resultFromValue, resultToValue; if (textblockFromValue.Text != "0") { Int32.TryParse(textblockFromValue.Text, out resultFromValue); if (resultFromValue == 0) { CallPopup("Некорректные\nданные в фильтре"); return; } } if (textblockToValue.Text != "0") { Int32.TryParse(textblockToValue.Text, out resultToValue); if (resultToValue == 0) { CallPopup("Некорректные\nданные в фильтре"); return; } } using (ApplicationContext db1 = new ApplicationContext()) { is_AcceptFilter = true; comboboxMonth.SelectedIndex = comboboxYear.SelectedIndex = 0; db1.Consumptions.Load(); db1.Types.Load(); db1.SubTypes.Load(); db1.Users.Load(); dynamic tempSubtype = comboboxSubType.Items[comboboxSubType.SelectedIndex]; int tempSubtypeId = tempSubtype.subTypeId; dynamic tempType = comboboxType.Items[comboboxType.SelectedIndex]; int tempTypeId = tempType.typeId; dynamic tempUser = comboboxUser.Items[comboboxUser.SelectedIndex]; int tempUserId = tempUser.userId; var resultGrid = from c in db1.Consumptions.Local join u in db1.Users.Local on c.UserId equals u.userId join s in db1.SubTypes.Local on c.SubTypeId equals s.subTypeId join t in db1.Types on s.TypeId equals t.typeId where (c.Description.Contains(textboxKeyWord.Text) == true || textboxKeyWord.Text == "") && ( Int32.Parse(textblockFromValue.Text) == 0 && Int32.Parse(textblockToValue.Text) == 0 || ( Int32.Parse(textblockToValue.Text) >= Int32.Parse(textblockFromValue.Text) && c.Value >= Int32.Parse(textblockFromValue.Text) && c.Value <= Int32.Parse(textblockToValue.Text) ) ) && ( (textblockFromPeriod.Text == "01.01.0001" && textblockToPeriod.Text == "01.01.0001" || textblockFromPeriod.Text == "1/1/0001" && textblockToPeriod.Text == "1/1/0001") || ( DateTime.Parse(textblockToPeriod.Text) >= DateTime.Parse(textblockFromPeriod.Text) && c.Date >= DateTime.Parse(textblockFromPeriod.Text) && c.Date <= DateTime.Parse(textblockToPeriod.Text) ) ) && (t.typeId == tempTypeId || comboboxType.SelectedIndex == 0) && (c.SubTypeId == tempSubtypeId || comboboxSubType.SelectedIndex == 0) && (c.UserId == tempUserId || comboboxUser.SelectedIndex == 0) select new { ConsumptionId = c.consumptionId, DescriptionConsumption = c.Description, TypeName = t.TypeName, SubTypeName = s.SubTypeName, SubTypeId = s.subTypeId, ValueConsumption = c.Value, ShowDate = c.Date.ToShortDateString(), DateConsumption = c.Date, UserName = u.Name + " " + u.Surname, UserId = u.userId }; var listResult = resultGrid.ToList(); this.DataContext = listResult; consumptionGrid.ItemsSource = listResult; ActiveFilter(); BindingMainWindow filtersElements = (BindingMainWindow)this.Resources["filtersElements"]; filtersElements.Sum = 0; foreach (var row in consumptionGrid.Items) { dynamic tempRow = row; filtersElements.Sum += tempRow.ValueConsumption; } is_FilterActivated = true; is_AcceptFilter = false; } var column = consumptionGrid.Columns[0]; consumptionGrid.Items.SortDescriptions.Clear(); consumptionGrid.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription(column.SortMemberPath, System.ComponentModel.ListSortDirection.Descending)); // Apply sort foreach (var col in consumptionGrid.Columns) { col.SortDirection = null; } column.SortDirection = System.ComponentModel.ListSortDirection.Descending; consumptionGrid.Items.Refresh(); }
public void RefreshDataGrid() { using (ApplicationContext db1 = new ApplicationContext()) { db1.Consumptions.Load(); db1.Types.Load(); db1.SubTypes.Load(); db1.Users.Load(); if (comboboxDay.SelectedIndex != 0 && comboboxMonth.SelectedIndex != 0) { var resultGrid = from c in db1.Consumptions.Local join u in db1.Users.Local on c.UserId equals u.userId join s in db1.SubTypes.Local on c.SubTypeId equals s.subTypeId join t in db1.Types on s.TypeId equals t.typeId where comboboxDay.SelectedIndex == c.Date.Day && comboboxMonth.SelectedIndex == c.Date.Month && comboboxYear.SelectedIndex + startYear - 1 == c.Date.Year select new { ConsumptionId = c.consumptionId, DescriptionConsumption = c.Description, TypeName = t.TypeName, SubTypeName = s.SubTypeName, SubTypeId = s.subTypeId, ValueConsumption = c.Value, DateConsumption = c.Date, ShowDate = c.Date.ToShortDateString(), UserName = u.Name + " " + u.Surname, UserId = u.userId }; var listResult = resultGrid.ToList(); this.DataContext = listResult; consumptionGrid.ItemsSource = listResult; } else if (comboboxDay.SelectedIndex == 0 && comboboxMonth.SelectedIndex != 0) { var resultGrid = from c in db1.Consumptions.Local join u in db1.Users.Local on c.UserId equals u.userId join s in db1.SubTypes.Local on c.SubTypeId equals s.subTypeId join t in db1.Types on s.TypeId equals t.typeId where comboboxMonth.SelectedIndex == c.Date.Month && comboboxYear.SelectedIndex + startYear - 1 == c.Date.Year select new { ConsumptionId = c.consumptionId, DescriptionConsumption = c.Description, TypeName = t.TypeName, SubTypeName = s.SubTypeName, SubTypeId = s.subTypeId, ValueConsumption = c.Value, DateConsumption = c.Date, ShowDate = c.Date.ToShortDateString(), UserName = u.Name + " " + u.Surname, UserId = u.userId }; var listResult = resultGrid.ToList(); this.DataContext = listResult; consumptionGrid.ItemsSource = listResult; } else if (comboboxYear.SelectedIndex != 0) { var resultGrid = from c in db1.Consumptions.Local join u in db1.Users.Local on c.UserId equals u.userId join s in db1.SubTypes.Local on c.SubTypeId equals s.subTypeId join t in db1.Types on s.TypeId equals t.typeId where comboboxYear.SelectedIndex + startYear - 1 == c.Date.Year select new { ConsumptionId = c.consumptionId, DescriptionConsumption = c.Description, TypeName = t.TypeName, SubTypeName = s.SubTypeName, SubTypeId = s.subTypeId, ValueConsumption = c.Value, DateConsumption = c.Date, ShowDate = c.Date.ToShortDateString(), UserName = u.Name + " " + u.Surname, UserId = u.userId }; var listResult = resultGrid.ToList(); this.DataContext = listResult; consumptionGrid.ItemsSource = listResult; } else { var resultGrid = from c in db1.Consumptions.Local join u in db1.Users.Local on c.UserId equals u.userId join s in db1.SubTypes.Local on c.SubTypeId equals s.subTypeId join t in db1.Types on s.TypeId equals t.typeId select new { ConsumptionId = c.consumptionId, DescriptionConsumption = c.Description, TypeName = t.TypeName, SubTypeName = s.SubTypeName, SubTypeId = s.subTypeId, ValueConsumption = c.Value, DateConsumption = c.Date, ShowDate = c.Date.ToShortDateString(), UserName = u.Name + " " + u.Surname, UserId = u.userId }; var listResult = resultGrid.ToList(); this.DataContext = listResult; consumptionGrid.ItemsSource = listResult; } var column = consumptionGrid.Columns[0]; consumptionGrid.Items.SortDescriptions.Clear(); consumptionGrid.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription(column.SortMemberPath, System.ComponentModel.ListSortDirection.Descending)); // Apply sort foreach (var col in consumptionGrid.Columns) { col.SortDirection = null; } column.SortDirection = System.ComponentModel.ListSortDirection.Descending; consumptionGrid.Items.Refresh(); if (!sum) { BindingMainWindow filtersElements = (BindingMainWindow)this.Resources["filtersElements"]; filtersElements.Sum = 0; foreach (var row in consumptionGrid.Items) { dynamic tempRow = row; filtersElements.Sum += tempRow.ValueConsumption; } sum = true; // DropFilter_Click(null, null); } } }