oee相关修改
This commit is contained in:
1 parent
9c5ef7bb31
commit
4419a4de3c
9 files changed
+422
-121
No files matched your search
+36
-16
@@ -38,10 +38,12 @@ namespace WindowsFormsApp1.form
|
||||
this.addresstypelegth = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.addressoffset = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.addressdecimal = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.remark = new System.Windows.Forms.DataGridViewTextBoxColumn();
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.查询 = new System.Windows.Forms.Button();
|
||||
this.保存 = new System.Windows.Forms.Button();
|
||||
this.清空 = new System.Windows.Forms.Button(); // 将保存改为清空
|
||||
this.关闭 = new System.Windows.Forms.Button();
|
||||
this.导入 = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
@@ -57,11 +59,12 @@ namespace WindowsFormsApp1.form
|
||||
this.addresstype,
|
||||
this.addresstypelegth,
|
||||
this.addressoffset,
|
||||
this.addressdecimal});
|
||||
this.addressdecimal,
|
||||
this.remark});
|
||||
this.dataGridView1.Location = new System.Drawing.Point(12, 41);
|
||||
this.dataGridView1.Name = "dataGridView1";
|
||||
this.dataGridView1.RowTemplate.Height = 23;
|
||||
this.dataGridView1.Size = new System.Drawing.Size(1044, 397);
|
||||
this.dataGridView1.Size = new System.Drawing.Size(1200, 397);
|
||||
this.dataGridView1.TabIndex = 0;
|
||||
//
|
||||
// id
|
||||
@@ -111,7 +114,6 @@ namespace WindowsFormsApp1.form
|
||||
this.addressoffset.DataPropertyName = "addressoffset";
|
||||
this.addressoffset.HeaderText = "地址偏移";
|
||||
this.addressoffset.Name = "addressoffset";
|
||||
|
||||
//
|
||||
// addressdecimal
|
||||
//
|
||||
@@ -119,6 +121,12 @@ namespace WindowsFormsApp1.form
|
||||
this.addressdecimal.HeaderText = "位地址";
|
||||
this.addressdecimal.Name = "addressdecimal";
|
||||
//
|
||||
// remark
|
||||
//
|
||||
this.remark.DataPropertyName = "remark";
|
||||
this.remark.HeaderText = "备注";
|
||||
this.remark.Name = "remark";
|
||||
//
|
||||
// button1
|
||||
//
|
||||
this.button1.Location = new System.Drawing.Point(12, 12);
|
||||
@@ -139,15 +147,15 @@ namespace WindowsFormsApp1.form
|
||||
this.查询.UseVisualStyleBackColor = true;
|
||||
this.查询.Click += new System.EventHandler(this.查询_Click);
|
||||
//
|
||||
// 保存
|
||||
// 清空
|
||||
//
|
||||
this.保存.Location = new System.Drawing.Point(174, 12);
|
||||
this.保存.Name = "保存";
|
||||
this.保存.Size = new System.Drawing.Size(75, 23);
|
||||
this.保存.TabIndex = 3;
|
||||
this.保存.Text = "保存";
|
||||
this.保存.UseVisualStyleBackColor = true;
|
||||
this.保存.Click += new System.EventHandler(this.保存_Click);
|
||||
this.清空.Location = new System.Drawing.Point(174, 12);
|
||||
this.清空.Name = "清空";
|
||||
this.清空.Size = new System.Drawing.Size(75, 23);
|
||||
this.清空.TabIndex = 3;
|
||||
this.清空.Text = "清空";
|
||||
this.清空.UseVisualStyleBackColor = true;
|
||||
this.清空.Click += new System.EventHandler(this.清空_Click);
|
||||
//
|
||||
// 关闭
|
||||
//
|
||||
@@ -159,13 +167,24 @@ namespace WindowsFormsApp1.form
|
||||
this.关闭.UseVisualStyleBackColor = true;
|
||||
this.关闭.Click += new System.EventHandler(this.关闭_Click);
|
||||
//
|
||||
// 导入
|
||||
//
|
||||
this.导入.Location = new System.Drawing.Point(336, 12);
|
||||
this.导入.Name = "导入";
|
||||
this.导入.Size = new System.Drawing.Size(75, 23);
|
||||
this.导入.TabIndex = 5;
|
||||
this.导入.Text = "导入";
|
||||
this.导入.UseVisualStyleBackColor = true;
|
||||
this.导入.Click += new System.EventHandler(this.导入_Click);
|
||||
//
|
||||
// OEEaddress_Manage
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(1068, 450);
|
||||
this.ClientSize = new System.Drawing.Size(1210, 450);
|
||||
this.Controls.Add(this.导入);
|
||||
this.Controls.Add(this.关闭);
|
||||
this.Controls.Add(this.保存);
|
||||
this.Controls.Add(this.清空);
|
||||
this.Controls.Add(this.查询);
|
||||
this.Controls.Add(this.button1);
|
||||
this.Controls.Add(this.dataGridView1);
|
||||
@@ -182,17 +201,18 @@ namespace WindowsFormsApp1.form
|
||||
private System.Windows.Forms.DataGridView dataGridView1;
|
||||
private System.Windows.Forms.Button button1;
|
||||
private System.Windows.Forms.Button 查询;
|
||||
private System.Windows.Forms.Button 保存;
|
||||
private System.Windows.Forms.Button 清空; // 将保存改为清空
|
||||
private System.Windows.Forms.Button 关闭;
|
||||
private System.Windows.Forms.Button 导入;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn id;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn plcid;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn code;
|
||||
private System.Windows.Forms.DataGridViewComboBoxColumn type;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn dbaddress;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn plcname;
|
||||
private System.Windows.Forms.DataGridViewComboBoxColumn addresstype;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn addresstypelegth;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn addressoffset;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn addressdecimal;
|
||||
private System.Windows.Forms.DataGridViewTextBoxColumn remark;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
@@ -24,28 +24,6 @@ namespace WindowsFormsApp1.form
|
||||
GetInfo();
|
||||
}
|
||||
|
||||
private void 保存_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||||
var result = sQLiteHelper.UpdateAll((List<t_oeeaddress>)dataGridView1.DataSource);
|
||||
if (result.Success)
|
||||
{
|
||||
MessageBox.Show("更新成功");
|
||||
GetInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("更新失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"更新失败,错误原因{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void 关闭_Click(object sender, EventArgs e)
|
||||
{
|
||||
this.Close();
|
||||
@@ -85,7 +63,7 @@ namespace WindowsFormsApp1.form
|
||||
comboBoxColumn.DisplayMember = "name";
|
||||
comboBoxColumn.ValueMember = "value";
|
||||
|
||||
comboBoxColumn.DataPropertyName = "type";
|
||||
comboBoxColumn.DataPropertyName = "type";
|
||||
|
||||
var comboBoxColumn1 = (DataGridViewComboBoxColumn)dataGridView1.Columns[5];
|
||||
// 绑定数据源到 ComboBox
|
||||
@@ -114,10 +92,10 @@ namespace WindowsFormsApp1.form
|
||||
dataGridView1.CellContentClick += DataGridView1_CellContentClick;
|
||||
|
||||
dataGridView1.Columns.Add(deleteColumn);
|
||||
|
||||
|
||||
GetInfo();
|
||||
}
|
||||
|
||||
|
||||
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
||||
{
|
||||
// 检查点击的是否为按钮列
|
||||
@@ -127,7 +105,7 @@ namespace WindowsFormsApp1.form
|
||||
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
|
||||
|
||||
int selectid = int.Parse(row.Cells[0].Value.ToString());
|
||||
if (e.ColumnIndex == 9)
|
||||
if (e.ColumnIndex == 10)
|
||||
{
|
||||
OEEaddress_Insert oeeInsert = new OEEaddress_Insert();
|
||||
oeeInsert.dataid = selectid;
|
||||
@@ -135,7 +113,7 @@ namespace WindowsFormsApp1.form
|
||||
oeeInsert.ShowDialog();
|
||||
GetInfo();
|
||||
}
|
||||
else if (e.ColumnIndex == 10)
|
||||
else if (e.ColumnIndex == 11)
|
||||
{
|
||||
DialogResult result = MessageBox.Show("你确定要删除数据吗?", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
|
||||
|
||||
@@ -156,7 +134,7 @@ namespace WindowsFormsApp1.form
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void GetInfo()
|
||||
{
|
||||
try
|
||||
@@ -200,5 +178,219 @@ namespace WindowsFormsApp1.form
|
||||
oeeInsert.ShowDialog();
|
||||
GetInfo();
|
||||
}
|
||||
|
||||
// 添加导入按钮点击事件处理
|
||||
private void 导入_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||
openFileDialog.Filter = "CSV文件|*.csv";
|
||||
openFileDialog.Title = "选择要导入的CSV文件";
|
||||
|
||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
string filePath = openFileDialog.FileName;
|
||||
ImportFromCSV(filePath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导入失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ImportFromCSV(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<t_oeeaddress> addressList = new List<t_oeeaddress>();
|
||||
|
||||
// 读取CSV文件 - 修改编码方式
|
||||
// 尝试使用UTF-8编码读取
|
||||
string[] lines = System.IO.File.ReadAllLines(filePath, Encoding.UTF8);
|
||||
|
||||
// 如果检测到乱码,可以尝试其他编码
|
||||
if (ContainsGarbledText(lines))
|
||||
{
|
||||
// 尝试使用GB2312编码
|
||||
lines = System.IO.File.ReadAllLines(filePath, Encoding.GetEncoding("gb2312"));
|
||||
|
||||
// 如果仍然乱码,再尝试其他编码
|
||||
if (ContainsGarbledText(lines))
|
||||
{
|
||||
// 尝试使用GBK编码
|
||||
lines = System.IO.File.ReadAllLines(filePath, Encoding.GetEncoding("gbk"));
|
||||
}
|
||||
}
|
||||
|
||||
// 从第2行开始读取数据(假设第1行是标题)
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string line = lines[i];
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
// 分割CSV行并去除双引号
|
||||
string[] values = SplitCsvLineAndRemoveQuotes(line);
|
||||
|
||||
if (values.Length < 10)
|
||||
{
|
||||
MessageBox.Show($"第{i + 1}行数据格式不正确,请检查CSV文件格式");
|
||||
break;
|
||||
}
|
||||
|
||||
var test = values[0];
|
||||
try
|
||||
{
|
||||
t_oeeaddress address = new t_oeeaddress
|
||||
{
|
||||
plcid = Convert.ToInt32(values[0]),
|
||||
code = values[1],
|
||||
type = values[2],
|
||||
dbaddress = values[3],
|
||||
addresstype = values[4],
|
||||
addresstypelegth = Convert.ToInt32(values[5]),
|
||||
addressoffset = Convert.ToInt32(values[6]),
|
||||
addressdecimal = Convert.ToInt32(values[7]),
|
||||
remark = values[8] ?? "",
|
||||
};
|
||||
|
||||
addressList.Add(address);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
MessageBox.Show($"第{i + 1}行数据格式转换错误,请检查数据类型是否正确");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SaveImportedData(addressList);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"CSV导入失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 添加一个方法来分割CSV行并去除双引号
|
||||
private string[] SplitCsvLineAndRemoveQuotes(string line)
|
||||
{
|
||||
// 使用正则表达式处理CSV,考虑引号内的逗号
|
||||
List<string> result = new List<string>();
|
||||
bool inQuotes = false;
|
||||
StringBuilder field = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < line.Length; i++)
|
||||
{
|
||||
char c = line[i];
|
||||
|
||||
if (c == '"')
|
||||
{
|
||||
// 如果是引号,切换引号状态
|
||||
inQuotes = !inQuotes;
|
||||
// 不将引号添加到结果中
|
||||
}
|
||||
else if (c == ',' && !inQuotes)
|
||||
{
|
||||
// 如果是逗号且不在引号内,则添加当前字段并重置
|
||||
result.Add(field.ToString().Trim());
|
||||
field.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他字符直接添加
|
||||
field.Append(c);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加最后一个字段
|
||||
result.Add(field.ToString().Trim());
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
// 添加一个辅助方法检测文本是否包含乱码
|
||||
private bool ContainsGarbledText(string[] lines)
|
||||
{
|
||||
// 简单检测:如果包含特定的乱码字符,则认为是乱码
|
||||
// 这里只是一个简单的示例,可能需要根据实际情况调整
|
||||
string pattern = @"[\u0000-\u001F\u007F-\u009F]"; // 控制字符
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (System.Text.RegularExpressions.Regex.IsMatch(line, pattern))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SaveImportedData(List<t_oeeaddress> addressList)
|
||||
{
|
||||
if (addressList.Count == 0)
|
||||
{
|
||||
MessageBox.Show("没有有效数据可导入");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||||
var result = sQLiteHelper.InsertAll(addressList);
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
// 更新缓存
|
||||
var allAddresses = sQLiteHelper.QueryAll();
|
||||
if (allAddresses.Success)
|
||||
{
|
||||
Init.t_Oeeaddresses = allAddresses.Data;
|
||||
}
|
||||
|
||||
MessageBox.Show($"成功导入{addressList.Count}条数据");
|
||||
GetInfo(); // 刷新数据显示
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"导入失败:{result.Msg}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"保存导入数据失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
// 添加清空按钮点击事件处理
|
||||
private void 清空_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
DialogResult result = MessageBox.Show("确定要删除所有数据吗?此操作不可恢复!", "警告",
|
||||
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
|
||||
|
||||
if (result == DialogResult.OK)
|
||||
{
|
||||
SQLiteHelper<t_oeeaddress> sQLiteHelper = new db.SQLiteHelper<t_oeeaddress>();
|
||||
var deleteResult = sQLiteHelper.DeleteAll();
|
||||
|
||||
if (deleteResult.Success)
|
||||
{
|
||||
MessageBox.Show("所有数据已成功删除!");
|
||||
GetInfo(); // 刷新数据显示
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"删除失败:{deleteResult.Msg}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"清空数据失败,错误原因:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,6 +107,13 @@ namespace WindowsFormsApp1.form
|
||||
var result = sQLiteHelper.Update(t_Oeeaddress);
|
||||
if (result.Success)
|
||||
{
|
||||
// 更新缓存
|
||||
var cacheIndex = Init.t_Oeeaddresses.FindIndex(x => x.id == dataid);
|
||||
if (cacheIndex != -1)
|
||||
{
|
||||
Init.t_Oeeaddresses[cacheIndex] = t_Oeeaddress;
|
||||
}
|
||||
|
||||
MessageBox.Show("修改成功");
|
||||
this.Close();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user