FrmOrder.cs
4.7 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Common;
using DeviceLib.BLL;
using DeviceLib.Model.AGV;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using UIControl.Forms;
namespace AGVDispatch
{
public partial class FrmOrder : FrmBase
{
public FrmOrder()
{
InitializeComponent();
dataGridView1.DataSource = OrderManager.Load();
DataAdapted();
OrderManager.OrderStateChanged += OrderManager_OrderStateChanged;
}
private void OrderManager_OrderStateChanged(List<Order> orders)
{
if (dataGridView1.InvokeRequired)
{
dataGridView1.Invoke(new Action(() =>
{
dataGridView1.DataSource = orders;
}));
}
else
{
dataGridView1.DataSource = orders;
}
DataAdapted();
}
private void DataAdapted()
{
dataGridView1.Columns[0].HeaderCell.Value = "编号";
dataGridView1.Columns[1].HeaderCell.Value = "类型";
dataGridView1.Columns[2].HeaderCell.Value = "状态";
dataGridView1.Columns[3].HeaderCell.Value = "机器人";
dataGridView1.Columns[4].HeaderCell.Value = "起始点";
dataGridView1.Columns[5].HeaderCell.Value = "目标点";
dataGridView1.Columns[6].HeaderCell.Value = "返还点";
dataGridView1.Columns[7].HeaderCell.Value = "备注";
//设置DataGridView文本居中
dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
}
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
//显示在HeaderCell上
try
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
DataGridViewRow r = this.dataGridView1.Rows[i];
r.HeaderCell.Value = string.Format("{0}", i + 1);
}
this.dataGridView1.Refresh();
}
catch { }
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = OrderManager.Load();
}
private void btnDelete_Click(object sender, EventArgs e)
{
int a = dataGridView1.CurrentRow.Index;
if (a == -1)
return;
string orderId = dataGridView1.Rows[a].Cells[0].Value?.ToString() ?? "";
DialogResult result = MessageBox.Show($"确定删除该订单号【{orderId}】", "提示", MessageBoxButtons.YesNo);
if (result.Equals(DialogResult.Yes))
{
OrderManager.DeleteOrderById(orderId, out string msg, true);
MessageBox.Show($"删除订单【{orderId}】【{msg}】", "操作结果");
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if (e == null || e.Value == null || !(sender is DataGridView))
return;
DataGridView dgv = sender as DataGridView;
if (dgv?.Columns[e.ColumnIndex].Name == "robot_id")
{
int value = (int)e.Value;
if(value == 0)
{
e.Value = "";
}
else
{
e.Value = RobotManager.GetRobot(value)?.name ?? value.ToString();
}
e.FormattingApplied = true;
}
else if (dgv?.Columns[e.ColumnIndex].Name == "type")
{
int value = (int)e.Value;
e.Value = OrderManager.GetOrderType(value);
e.FormattingApplied = true;
}
else if (dgv?.Columns[e.ColumnIndex].Name == "state")
{
int value = (int)e.Value;
e.Value = OrderManager.GetOrderState(value);
e.FormattingApplied = true;
}
}
catch (Exception ex)
{
LogUtil.Error($"订单单元格自定义格式失败", ex);
}
}
}
}