-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSummaryObserver.cs
More file actions
66 lines (55 loc) · 1.78 KB
/
Copy pathSummaryObserver.cs
File metadata and controls
66 lines (55 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PDFiller
{
internal class SummaryObserver : IObserver<Shipment>
{
private DataGridView _dataGridView;
public SummaryObserver(DataGridView dataGridView) {
this._dataGridView = dataGridView;
this._dataGridView.Rows.Clear();
}
public void OnCompleted()
{
_dataGridView.Rows.Clear();
}
public void OnError(Exception error)
{
_dataGridView.Rows.Clear();
_dataGridView.Rows.Add(null,error.Message);
}
public void OnNext(Shipment shipment)
{
_dataGridView.Rows.Clear();
Dictionary<string, int> dict = new Dictionary<string, int>();
var orders = shipment.Orders;
foreach (Order o in orders)
{
foreach (SoldProduct product in o.products)
{
//KeyValuePair<string, string> key = new KeyValuePair<string, string>(tp.tName, tp.tId);
string key = product.Name;
if (dict.ContainsKey(key))
{
dict[key] += product.Quantity;
}
else
{
dict[key] = product.Quantity;
}
}
}
foreach (var pair in dict)
{
_dataGridView.Rows.Add(pair.Value, pair.Key);
}
_dataGridView.Sort(_dataGridView.Columns[0], ListSortDirection.Descending);
}
}
}