diff --git a/全自动水压检测仪/DATA/ConductividyClass.cs b/全自动水压检测仪/DATA/ConductividyClass.cs index 0f3bf6c..cb46a11 100644 --- a/全自动水压检测仪/DATA/ConductividyClass.cs +++ b/全自动水压检测仪/DATA/ConductividyClass.cs @@ -15,6 +15,8 @@ namespace 材料热传导系数 { public int Id { get; set; } + public int row { get; set; } + /// /// 条码(保留用于兼容性) /// @@ -266,15 +268,73 @@ standarderror) } } - public List GetScanDataBylldh_jh(string lldh, string jh) + public List GetScanDataBylldh_jh(string jh) { using (var connection = new MySqlConnection(_connectionString)) { connection.Open(); var sql = @"SELECT * FROM scandata - where lldh=@lldh and jh = @jh + where jh = @jh ORDER BY id asc "; - return connection.Query(sql, new { @lldh, jh }).ToList(); + return connection.Query(sql, new { jh }).ToList(); + } + } + + /// + /// 更新扫描数据 + /// + /// 要更新的扫描数据 + public void UpdateScanItem(ScanData scanData) + { + using (var connection = new MySqlConnection(_connectionString)) + { + connection.Open(); + var sql = @"UPDATE scandata SET + barcode = @barcode, + diffpressure = @diffpressure, + exit_temperature = @exit_temperature, + temperature = @temperature, + dwelltime = @dwelltime, + TemperatureMode = @TemperatureMode, + kzh = @kzh, + lldh = @lldh, + jh = @jh, + quantity = @quantity, + pressuresetting = @pressuresetting, + standarderror = @standarderror + WHERE id = @Id"; + + connection.Execute(sql, new + { + scanData.barcode, + scanData.diffpressure, + scanData.exit_temperature, + scanData.temperature, + scanData.dwelltime, + scanData.TemperatureMode, + scanData.kzh, + scanData.lldh, + scanData.jh, + scanData.quantity, + scanData.pressuresetting, + scanData.standarderror, + scanData.Id + }); + } + } + + /// + /// 根据ID获取扫描数据 + /// + /// 数据ID + /// 扫描数据对象 + public ScanData GetScanDataById(int id) + { + using (var connection = new MySqlConnection(_connectionString)) + { + connection.Open(); + var sql = @"SELECT * FROM scandata WHERE id = @id"; + return connection.QueryFirstOrDefault(sql, new { id }); } } @@ -293,5 +353,53 @@ standarderror) return false; } } + + + + /// + /// 根据ID获取测试数据 + /// + public ConductivityTestData GetTestDataById(int id) + { + using (var connection = new MySqlConnection(_connectionString)) + { + connection.Open(); + var sql = @"SELECT * FROM normaltemperature WHERE id = @id"; + return connection.QueryFirstOrDefault(sql, new { id }); + } + } + + /// + /// 更新测试数据 + /// + public void UpdateTestData(ConductivityTestData testData) + { + using (var connection = new MySqlConnection(_connectionString)) + { + connection.Open(); + var sql = @"UPDATE normaltemperature SET + barcode = @barcode, + temperature = @temperature, + startpressure = @startpressure, + dwelltime = @dwelltime, + diffpressure = @diffpressure, + endpressure = @endpressure, + type = @type, + kzh = @kzh, + starttime = @starttime, + endtime = @endtime, + lldh = @lldh, + jh = @jh, + quantity = @quantity, + standarderror = @standarderror, + testresult = @testresult + WHERE id = @Id"; + + connection.Execute(sql, testData); + } + } + + + } } diff --git a/全自动水压检测仪/DATA/Function.cs b/全自动水压检测仪/DATA/Function.cs index 504d1b1..1318848 100644 --- a/全自动水压检测仪/DATA/Function.cs +++ b/全自动水压检测仪/DATA/Function.cs @@ -77,7 +77,7 @@ namespace 全自动水压检测仪 { int maxRetries = 3; int retryCount = 0; - + while (retryCount < maxRetries) { try @@ -106,7 +106,7 @@ namespace 全自动水压检测仪 // 如果读取失败,默认写入true(开启状态) System.Diagnostics.Debug.WriteLine($"[Function] 读取地址{address}失败,使用默认值: {readEx.Message}"); } - + // 写入相反的状态 modbusMaster.WriteSingleCoil(1, address, !currentState); Thread.Sleep(100); @@ -122,7 +122,7 @@ namespace 全自动水压检测仪 default: break; } - + // 操作成功,退出重试循环 return; } @@ -130,13 +130,13 @@ namespace 全自动水压检测仪 { retryCount++; System.Diagnostics.Debug.WriteLine($"[Function] Modbus从站异常 (尝试 {retryCount}/{maxRetries}): {slaveEx.Message}"); - + if (retryCount >= maxRetries) { MessageBox.Show($"操作失败!\n\nModbus从站错误: {slaveEx.SlaveExceptionCode}\n{slaveEx.Message}", "错误"); return; } - + // 等待后重试 Thread.Sleep(200); } @@ -144,13 +144,13 @@ namespace 全自动水压检测仪 { retryCount++; System.Diagnostics.Debug.WriteLine($"[Function] 操作失败 (尝试 {retryCount}/{maxRetries}): {ex.Message}"); - + if (retryCount >= maxRetries) { MessageBox.Show("操作失败!" + "\n" + "\n" + ex.Message, "错误"); return; } - + // 等待后重试 Thread.Sleep(200); } @@ -190,7 +190,7 @@ namespace 全自动水压检测仪 } - public void WriteToPLCForNew(string inPutValue, ushort address, DataType dataType, bool isok=false,float max=0,float min=0) + public void WriteToPLCForNew(string inPutValue, ushort address, DataType dataType, bool isok = false, float max = 0, float min = 0) { try { @@ -202,7 +202,7 @@ namespace 全自动水压检测仪 if (UIInputDialog.ShowInputDoubleDialog(ref value, UIStyle.Inherited, desc: "请输入值", showMask: false)) { - if ( isok&&value > max || value < min) + if (isok && value > max || value < min) { MessageBox.Show("数据不正确"); return; @@ -212,9 +212,13 @@ namespace 全自动水压检测仪 break; case DataType.整形: int value_int = inPutValue.ToInt(); + if (UIInputDialog.ShowInputIntegerDialog(ref value_int, UIStyle.Inherited, desc: "请输入数据:")) { - + if (isok) + { + value_int = value_int * 10; + } modbusMaster.WriteMultipleRegisters(1, address, dc.intToushorts(value_int)); //if (isok) //{ modbusMaster.WriteSingleCoil(1, 25, true); } diff --git a/全自动水压检测仪/NormalTemperatureMode.Designer.cs b/全自动水压检测仪/NormalTemperatureMode.Designer.cs index 0701088..fa63c53 100644 --- a/全自动水压检测仪/NormalTemperatureMode.Designer.cs +++ b/全自动水压检测仪/NormalTemperatureMode.Designer.cs @@ -171,6 +171,8 @@ namespace 全自动水压检测仪 this.uiPanel2 = new Sunny.UI.UIPanel(); this.uiLabel1 = new Sunny.UI.UILabel(); this.uiLight1 = new Sunny.UI.UILight(); + this.uiLabel26 = new Sunny.UI.UILabel(); + this.uiTextBox14 = new Sunny.UI.UITextBox(); this.uiTableLayoutPanel1.SuspendLayout(); this.uiPanel1.SuspendLayout(); this.uiPanel4.SuspendLayout(); @@ -1448,6 +1450,8 @@ namespace 全自动水压检测仪 // // uiPanel14 // + this.uiPanel14.Controls.Add(this.uiLabel26); + this.uiPanel14.Controls.Add(this.uiTextBox14); this.uiPanel14.Controls.Add(this.uiLabel25); this.uiPanel14.Controls.Add(this.uiTextBox13); this.uiPanel14.Controls.Add(this.uiLabel24); @@ -1630,6 +1634,7 @@ namespace 全自动水压检测仪 this.uiLabel35.TabIndex = 2; this.uiLabel35.Text = "联络单号:"; this.uiLabel35.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + this.uiLabel35.Visible = false; // // uiLabel49 // @@ -1668,6 +1673,7 @@ namespace 全自动水压检测仪 this.uiTextBox2.Size = new System.Drawing.Size(128, 28); this.uiTextBox2.TabIndex = 3; this.uiTextBox2.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter; + this.uiTextBox2.Visible = false; this.uiTextBox2.Watermark = ""; // // uiTextBox10 @@ -2376,6 +2382,35 @@ namespace 全自动水压检测仪 this.uiLight1.TabIndex = 1; this.uiLight1.Text = "uiLight1"; // + // uiLabel26 + // + this.uiLabel26.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.uiLabel26.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(48)))), ((int)(((byte)(48))))); + this.uiLabel26.Location = new System.Drawing.Point(277, 135); + this.uiLabel26.Name = "uiLabel26"; + this.uiLabel26.Size = new System.Drawing.Size(112, 30); + this.uiLabel26.TabIndex = 25; + this.uiLabel26.Text = "水循环时间设置:"; + this.uiLabel26.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // + // uiTextBox14 + // + this.uiTextBox14.Cursor = System.Windows.Forms.Cursors.IBeam; + this.uiTextBox14.Font = new System.Drawing.Font("微软雅黑", 10.5F); + this.uiTextBox14.Location = new System.Drawing.Point(395, 134); + this.uiTextBox14.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.uiTextBox14.MinimumSize = new System.Drawing.Size(1, 16); + this.uiTextBox14.Name = "uiTextBox14"; + this.uiTextBox14.Padding = new System.Windows.Forms.Padding(5); + this.uiTextBox14.Radius = 3; + this.uiTextBox14.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(220)))), ((int)(((byte)(220))))); + this.uiTextBox14.ShowText = false; + this.uiTextBox14.Size = new System.Drawing.Size(91, 28); + this.uiTextBox14.TabIndex = 26; + this.uiTextBox14.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter; + this.uiTextBox14.Watermark = ""; + this.uiTextBox14.Click += new System.EventHandler(this.uiTextBox14_Click); + // // NormalTemperatureMode // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; @@ -2590,6 +2625,8 @@ namespace 全自动水压检测仪 private Sunny.UI.UITextBox uiTextBox13; private Sunny.UI.UILabel uiLabel24; private Sunny.UI.UITextBox uiTextBox12; + private Sunny.UI.UILabel uiLabel26; + private Sunny.UI.UITextBox uiTextBox14; } } diff --git a/全自动水压检测仪/NormalTemperatureMode.cs b/全自动水压检测仪/NormalTemperatureMode.cs index e38ca91..37a9974 100644 --- a/全自动水压检测仪/NormalTemperatureMode.cs +++ b/全自动水压检测仪/NormalTemperatureMode.cs @@ -394,7 +394,7 @@ namespace 全自动水压检测仪 if (currentValue > settingValue) { isValid = false; - + _modbusMaster.WriteSingleCoil(1, 10082, true); timer?.Stop(); MessageBox.Show("实验已达到设置压差,自动停止!"); @@ -450,7 +450,7 @@ namespace 全自动水压检测仪 Real13 = SafelyReadRegisters(1, 3484, 2),// 温度保护 Real14 = SafelyReadRegisters(1, 3134, 2),// 压力保护 - + Real15 = SafelyReadRegisters(1, 2406, 1),// 时间 CurrentTime = DateTime.Now }; } @@ -617,6 +617,15 @@ namespace 全自动水压检测仪 uiTextBox13.Text = propre.ToString("F1"); } + + // 压力保护 + if (uiTextBox14 != null && !uiTextBox14.IsDisposed && + modbusData.Real15 != null && modbusData.Real15.Length >= 2) + { + var propre = modbusData.Real15[0]; + uiTextBox14.Text = (propre / 10).ToString("F1"); + } + } catch (Exception uiEx) { @@ -1040,7 +1049,7 @@ namespace 全自动水压检测仪 } string plcIp = "192.168.1.10"; - // string plcIp = "127.0.0.1"; + //string plcIp = "127.0.0.1"; bool initSuccess = Data.ModbusResourceManager.Instance.Init(plcIp, 502); if (!initSuccess) { @@ -1611,14 +1620,14 @@ namespace 全自动水压检测仪 itemNumber = uiTextBox10.Text.Trim(); }); - if (string.IsNullOrEmpty(contactNumber)) - { - SafeInvoke(() => - { - MessageBox.Show("请输入联络单号!"); - }); - return; - } + //if (string.IsNullOrEmpty(contactNumber)) + //{ + // SafeInvoke(() => + // { + // MessageBox.Show("请输入联络单号!"); + // }); + // return; + //} if (string.IsNullOrEmpty(itemNumber)) { @@ -1635,14 +1644,14 @@ namespace 全自动水压检测仪 // 2. 从数据库查询数据 - ScanData scanData = GetScanDataByBarcode(lldh, jh); + ScanData scanData = GetScanDataByBarcode(jh); if (scanData == null) { SafeInvoke(() => { - MessageBox.Show($"未找到联络单号 {contactNumber} 和件号 {itemNumber} 的记录!"); + MessageBox.Show($"未找到件号 {itemNumber} 的记录!"); }); return; } @@ -1676,17 +1685,17 @@ namespace 全自动水压检测仪 } - private ScanData GetScanDataByBarcode(string lldh, string jh) + private ScanData GetScanDataByBarcode(string jh) { using (var connection = new MySqlConnection(_repository._connectionString)) { connection.Open(); var sql = @"SELECT * FROM scandata - WHERE lldh = @lldh and jh = @jh + WHERE jh = @jh ORDER BY CreateTime DESC LIMIT 1"; - return connection.QueryFirstOrDefault(sql, new { lldh, jh }); + return connection.QueryFirstOrDefault(sql, new { jh }); } } @@ -1937,5 +1946,14 @@ namespace 全自动水压检测仪 ma?.WriteToPLCForNew(uiTextBox13.Text.Trim(), 3134, Function.DataType.浮点型); }); } + + private void uiTextBox14_Click(object sender, EventArgs e) + { + SafeInvoke(() => + { + + ma?.WriteToPLCForNew(uiTextBox14.Text.Trim(), 2406, Function.DataType.整形, isok: true); + }); + } } } \ No newline at end of file diff --git a/全自动水压检测仪/Report.cs b/全自动水压检测仪/Report.cs index 006c384..1292ec4 100644 --- a/全自动水压检测仪/Report.cs +++ b/全自动水压检测仪/Report.cs @@ -119,12 +119,38 @@ namespace 全自动水压检测仪 { dataGridView.Columns.Clear(); dataGridView.ScrollBars = ScrollBars.Both; + + + // ============ 添加编辑按钮列(放在第一列) ============ + DataGridViewButtonColumn editColumn = new DataGridViewButtonColumn + { + Name = "EditColumn", + HeaderText = "操作", + Text = "保存", + UseColumnTextForButtonValue = true, + Width = 60, + SortMode = DataGridViewColumnSortMode.NotSortable, + DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter } + }; + dataGridView.Columns.Add(editColumn); + dataGridView.Columns.Add(new DataGridViewTextBoxColumn { Name = "Id", HeaderText = "编号", Width = 50, - DataPropertyName = "Id", // 绑定到ConductivityTestData的Id属性 + DataPropertyName = "Id", + SortMode = DataGridViewColumnSortMode.NotSortable, + DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter } + }); + + + dataGridView.Columns.Add(new DataGridViewTextBoxColumn + { + Name = "Id", + HeaderText = "序号", + Width = 50, + DataPropertyName = "row", // 绑定到ConductivityTestData的Id属性 SortMode = DataGridViewColumnSortMode.NotSortable, DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter } }); @@ -469,21 +495,66 @@ namespace 全自动水压检测仪 } } + //private void Report_Load(object sender, EventArgs e) + //{ + // // 检查数据源 + // //if (CurrentReport == null || CurrentReport.Count == 0) + // //{ + // // MessageBox.Show("没有测试数据可显示", "提示", + // // MessageBoxButtons.OK, MessageBoxIcon.Information); + // // return; + // //} + + // try + // { + // dataGridView.CellFormatting += DataGridView_CellFormatting; + + + + // var result = _repository.GetTestData(); + // if (result != null && result.Count > 0) + // { + // CurrentReport = result; + // } + // int i = 0; + // CurrentReport.ForEach(x => + // { + // i++; + // x.Id = i; + // }); + // dataGridView.DataSource = CurrentReport; + + // // 刷新显示 + // dataGridView.Refresh(); + + // this.Text = $"测试数据报表(共{CurrentReport.Count}条记录)"; + // } + // catch (Exception ex) + // { + // MessageBox.Show($"加载数据失败:{ex.Message}", "错误", + // MessageBoxButtons.OK, MessageBoxIcon.Error); + // } + //} private void Report_Load(object sender, EventArgs e) { - // 检查数据源 - //if (CurrentReport == null || CurrentReport.Count == 0) - //{ - // MessageBox.Show("没有测试数据可显示", "提示", - // MessageBoxButtons.OK, MessageBoxIcon.Information); - // return; - //} - try { dataGridView.CellFormatting += DataGridView_CellFormatting; + // ============ 启用DataGridView编辑功能 ============ + dataGridView.ReadOnly = false; + dataGridView.AllowUserToAddRows = false; + dataGridView.EditMode = DataGridViewEditMode.EditOnEnter; + // 确保除了Id列和编辑按钮列外都可编辑 + foreach (DataGridViewColumn column in dataGridView.Columns) + { + if (column.Name != "Id" && column.Name != "EditColumn") + { + column.ReadOnly = false; + } + } + // ============ 结束设置 ============ var result = _repository.GetTestData(); if (result != null && result.Count > 0) @@ -494,7 +565,7 @@ namespace 全自动水压检测仪 CurrentReport.ForEach(x => { i++; - x.Id = i; + x.row = i; }); dataGridView.DataSource = CurrentReport; @@ -502,6 +573,11 @@ namespace 全自动水压检测仪 dataGridView.Refresh(); this.Text = $"测试数据报表(共{CurrentReport.Count}条记录)"; + + // ============ 添加编辑按钮点击事件 ============ + dataGridView.CellClick -= DataGridViewCellClickHandler; // 先移除避免重复 + dataGridView.CellClick += DataGridViewCellClickHandler; + // ============ 结束添加 ============ } catch (Exception ex) { @@ -510,8 +586,99 @@ namespace 全自动水压检测仪 } } + // 编辑按钮点击事件处理 + private void DataGridViewCellClickHandler(object sender, DataGridViewCellEventArgs e) + { + // 确保点击的是编辑按钮列 + if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && + dataGridView.Columns[e.ColumnIndex].Name == "EditColumn") + { + try + { + // 结束当前单元格的编辑 + dataGridView.EndEdit(); + // 获取当前行的数据 + var row = dataGridView.Rows[e.RowIndex]; + // 验证必要字段 + if (row.Cells["jh"].Value == null || string.IsNullOrEmpty(row.Cells["jh"].Value.ToString())) + { + MessageBox.Show("件号不能为空!", "验证错误", + MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + + // 从数据库获取原始数据(通过Id) + int id = Convert.ToInt32(row.Cells["Id"].Value); + var originalData = _repository.GetTestDataById(id); + + if (originalData == null) + { + MessageBox.Show("未找到对应的测试数据!", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + // 更新数据 + originalData.lldh = row.Cells["lldh"].Value?.ToString() ?? ""; + originalData.jh = row.Cells["jh"].Value?.ToString() ?? ""; + originalData.kzh = row.Cells["kzh"].Value?.ToString() ?? ""; + + // 数值类型(带验证) + if (row.Cells["quantity"].Value != null && !string.IsNullOrEmpty(row.Cells["quantity"].Value.ToString())) + originalData.quantity = Convert.ToInt32(row.Cells["quantity"].Value); + + if (row.Cells["startpressure"].Value != null && !string.IsNullOrEmpty(row.Cells["startpressure"].Value.ToString())) + originalData.startpressure = Convert.ToDouble(row.Cells["startpressure"].Value); + + if (row.Cells["endpressure"].Value != null && !string.IsNullOrEmpty(row.Cells["endpressure"].Value.ToString())) + originalData.endpressure = Convert.ToDouble(row.Cells["endpressure"].Value); + + if (row.Cells["dwelltime"].Value != null && !string.IsNullOrEmpty(row.Cells["dwelltime"].Value.ToString())) + originalData.dwelltime = Convert.ToDouble(row.Cells["dwelltime"].Value); + + if (row.Cells["diffpressure"].Value != null && !string.IsNullOrEmpty(row.Cells["diffpressure"].Value.ToString())) + originalData.diffpressure = Convert.ToDouble(row.Cells["diffpressure"].Value); + + if (row.Cells["temperature"].Value != null && !string.IsNullOrEmpty(row.Cells["temperature"].Value.ToString())) + originalData.temperature = Convert.ToDouble(row.Cells["temperature"].Value); + + if (row.Cells["standarderror"].Value != null && !string.IsNullOrEmpty(row.Cells["standarderror"].Value.ToString())) + originalData.standarderror = Convert.ToDecimal(row.Cells["standarderror"].Value); + + originalData.testresult = row.Cells["testresult"].Value?.ToString() ?? ""; + + // 类型转换 + string typeValue = row.Cells["Type"].Value?.ToString() ?? ""; + if (typeValue == "常温") + originalData.Type = 1; + else if (typeValue == "高温") + originalData.Type = 0; + + // 保存到数据库 + _repository.UpdateTestData(originalData); + + MessageBox.Show("修改已保存!", "成功", + MessageBoxButtons.OK, MessageBoxIcon.Information); + + // 刷新数据(可选) + // var result = _repository.GetTestData(); + // CurrentReport = result; + // dataGridView.DataSource = CurrentReport; + } + catch (FormatException) + { + MessageBox.Show("数值格式错误,请检查输入的数据!", "格式错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + catch (Exception ex) + { + MessageBox.Show($"保存失败:{ex.Message}", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } /// /// 导出报表按钮点击事件 /// @@ -940,6 +1107,7 @@ namespace 全自动水压检测仪 { //base.OnFormClosing(e); dataGridView.CellFormatting -= DataGridView_CellFormatting; + dataGridView.CellClick -= DataGridViewCellClickHandler; // 移除事件 } } } diff --git a/全自动水压检测仪/ScanImport.Designer.cs b/全自动水压检测仪/ScanImport.Designer.cs index 1d23830..dc59c81 100644 --- a/全自动水压检测仪/ScanImport.Designer.cs +++ b/全自动水压检测仪/ScanImport.Designer.cs @@ -196,6 +196,7 @@ this.uiTextBox2.Style = Sunny.UI.UIStyle.Custom; this.uiTextBox2.TabIndex = 12; this.uiTextBox2.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft; + this.uiTextBox2.Visible = false; this.uiTextBox2.Watermark = "请输入联络单号"; // // uiTextBox11 @@ -259,6 +260,7 @@ this.uiLabel50.TabIndex = 31; this.uiLabel50.Text = "联络单号:"; this.uiLabel50.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + this.uiLabel50.Visible = false; // // uiLabel51 // @@ -675,6 +677,7 @@ this.ColumnContactNumber.MinimumWidth = 6; this.ColumnContactNumber.Name = "ColumnContactNumber"; this.ColumnContactNumber.ReadOnly = true; + this.ColumnContactNumber.Visible = false; this.ColumnContactNumber.Width = 150; // // ColumnItemNumber diff --git a/全自动水压检测仪/ScanImport.cs b/全自动水压检测仪/ScanImport.cs index 154bcd2..2398fdd 100644 --- a/全自动水压检测仪/ScanImport.cs +++ b/全自动水压检测仪/ScanImport.cs @@ -89,11 +89,11 @@ namespace 全自动水压检测仪 string itemNumber = uiTextBox11?.Text?.Trim() ?? ""; string engravingNumber = uiTextBoxEngravingNumber?.Text?.Trim() ?? ""; - if (string.IsNullOrEmpty(contactNumber)) - { - MessageBox.Show("请输入联络单号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); - return; - } + //if (string.IsNullOrEmpty(contactNumber)) + //{ + // MessageBox.Show("请输入联络单号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); + // return; + //} if (string.IsNullOrEmpty(itemNumber)) { @@ -106,10 +106,10 @@ namespace 全自动水压检测仪 - bool isAny = _repository.GetScanDataBylldh_jh(uiTextBox2.Text, uiTextBox11.Text).Any(); + bool isAny = _repository.GetScanDataBylldh_jh(uiTextBox11.Text).Any(); if (isAny) { - MessageBox.Show("联络单号-件号重复"); + MessageBox.Show("件号重复"); return; } try @@ -189,46 +189,46 @@ namespace 全自动水压检测仪 } }); } - private void LoadData() - { - try - { - // 将数据查询操作移到后台线程,避免UI卡顿 - System.Threading.Tasks.Task.Run(() => - { - var data = _repository.GetScanData(); + //private void LoadData() + //{ + // try + // { + // // 将数据查询操作移到后台线程,避免UI卡顿 + // System.Threading.Tasks.Task.Run(() => + // { + // var data = _repository.GetScanData(); - // 安全更新DataGridView - SafeInvoke(() => - { - // 避免重复绑定导致的异常 - if (uiDataGridView1 != null && !uiDataGridView1.IsDisposed) - { - // 先解绑再重新绑定 - uiDataGridView1.DataSource = new List(); - uiDataGridView1.DataSource = data; + // // 安全更新DataGridView + // SafeInvoke(() => + // { + // // 避免重复绑定导致的异常 + // if (uiDataGridView1 != null && !uiDataGridView1.IsDisposed) + // { + // // 先解绑再重新绑定 + // uiDataGridView1.DataSource = new List(); + // uiDataGridView1.DataSource = data; - if (uiDataGridView1.Columns.Contains("Column1")) - { - uiDataGridView1.Columns["Column1"].Visible = false; - } + // if (uiDataGridView1.Columns.Contains("Column1")) + // { + // uiDataGridView1.Columns["Column1"].Visible = false; + // } - SetRowNumbers(); - uiDataGridView1.Refresh(); + // SetRowNumbers(); + // uiDataGridView1.Refresh(); - } - }); - }); - } - catch (Exception ex) - { - SafeInvoke(() => - { - MessageBox.Show($"加载数据失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); - }); - } - } + // } + // }); + // }); + // } + // catch (Exception ex) + // { + // SafeInvoke(() => + // { + // MessageBox.Show($"加载数据失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + // }); + // } + //} //private void SwitchWindow(ref T windowInstance, Func createFunc) where T : UIForm //{ @@ -252,7 +252,134 @@ namespace 全自动水压检测仪 // this.Hide(); // windowInstance.Show(); //} + private void LoadData() + { + try + { + // 将数据查询操作移到后台线程,避免UI卡顿 + System.Threading.Tasks.Task.Run(() => + { + var data = _repository.GetScanData(); + // 安全更新DataGridView + SafeInvoke(() => + { + // 避免重复绑定导致的异常 + if (uiDataGridView1 != null && !uiDataGridView1.IsDisposed) + { + // ============ 启用编辑功能 ============ + // 关键设置:允许用户编辑 + uiDataGridView1.ReadOnly = false; + uiDataGridView1.AllowUserToAddRows = false; + uiDataGridView1.AllowUserToDeleteRows = false; + uiDataGridView1.EditMode = DataGridViewEditMode.EditOnEnter; // 或 EditOnKeystrokeOrF2 + // ============ 结束设置 ============ + + // 先解绑再重新绑定 + uiDataGridView1.DataSource = new List(); + uiDataGridView1.DataSource = data; + + // ============ 添加编辑按钮列 ============ + // 检查是否已存在编辑按钮列 + if (!uiDataGridView1.Columns.Contains("EditButton")) + { + DataGridViewButtonColumn editButtonColumn = new DataGridViewButtonColumn(); + editButtonColumn.Name = "EditButton"; + editButtonColumn.HeaderText = "保存修改"; + editButtonColumn.Text = "保存"; + editButtonColumn.UseColumnTextForButtonValue = true; + editButtonColumn.Width = 80; + + // 将按钮列添加到DataGridView中(放在第一列) + uiDataGridView1.Columns.Add(editButtonColumn); + uiDataGridView1.Columns["EditButton"].DisplayIndex = 0; // 放在第一列 + } + + // ============ 设置各列属性,使数据可编辑 ============ + // 确保所有列都可编辑(除了Id列和按钮列) + foreach (DataGridViewColumn column in uiDataGridView1.Columns) + { + if (column.Name != "Id" && column.Name != "EditButton") + { + column.ReadOnly = false; + + // 特别设置数值列的编辑属性 + if (column.Name.Contains("pressure") || + column.Name.Contains("temperature") || + column.Name.Contains("dwelltime") || + column.Name.Contains("quantity") || + column.Name.Contains("standarderror")) + { + // 设置单元格为文本框,可以编辑数值 + if (column is DataGridViewTextBoxColumn textBoxColumn) + { + textBoxColumn.DefaultCellStyle.Format = "F2"; // 显示两位小数 + } + } + } + } + + // 隐藏ID列(如果需要) + if (uiDataGridView1.Columns.Contains("Id")) + { + uiDataGridView1.Columns["Id"].Visible = false; + } + + // ============ 添加按钮点击事件处理 ============ + // 移除旧的事件处理程序(避免重复) + uiDataGridView1.CellClick -= DataGridViewCellClickHandler; + // 添加新的事件处理程序 + uiDataGridView1.CellClick += DataGridViewCellClickHandler; + // ============ 结束添加 ============ + + SetRowNumbers(); + uiDataGridView1.Refresh(); + } + }); + }); + } + catch (Exception ex) + { + SafeInvoke(() => + { + MessageBox.Show($"加载数据失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + }); + } + } + // 编辑按钮点击事件处理 + private void DataGridViewCellClickHandler(object sender, DataGridViewCellEventArgs e) + { + // 确保点击的是按钮列 + if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && + uiDataGridView1.Columns[e.ColumnIndex].Name == "EditButton") + { + // 获取选中的行数据 + if (uiDataGridView1.Rows[e.RowIndex].DataBoundItem is ScanData scanData) + { + // 保存修改到数据库 + try + { + bool isAny = _repository.GetScanDataBylldh_jh(scanData.jh).Any(); + if (isAny) + { + MessageBox.Show("件号重复"); + return; + } + + _repository.UpdateScanItem(scanData); + + MessageBox.Show("修改已保存!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); + + // 刷新数据 + LoadData(); + } + catch (Exception ex) + { + MessageBox.Show($"保存失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } private void SwitchWindow(T windowInstance, Func createFunc) where T : UIForm {