diff --git a/WindowsFormsApp6/Form3.cs b/WindowsFormsApp6/Form3.cs index eea9681..8cc99ba 100644 --- a/WindowsFormsApp6/Form3.cs +++ b/WindowsFormsApp6/Form3.cs @@ -37,6 +37,7 @@ namespace WindowsFormsApp6 private DataTable sampleDataTable; private int currentSampleCount = 5; // 当前试样数量,可动态调整 private readonly Random random = new Random(); + private bool isVerticalLayout = true; // true=纵向(当前),false=横向 #endregion public Form3() @@ -69,7 +70,7 @@ namespace WindowsFormsApp6 } /// - /// 初始化 DataGridView 列 - 实现2级表头 + /// 初始化 DataGridView 列 - 实现2级表头(纵向)或普通表头(横向) /// private void InitializeDataGridView() { @@ -91,49 +92,15 @@ namespace WindowsFormsApp6 Alignment = DataGridViewContentAlignment.MiddleCenter }; - // 序号列 - dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + if (isVerticalLayout) { - Name = "序号", - HeaderText = "序号", - DataPropertyName = "序号", - Width = 180, - ReadOnly = true, - DefaultCellStyle = centerStyle - }); - - // 为每个试样添加3列(2级表头:试样N + 子列1/2/3) - for (int i = 1; i <= currentSampleCount; i++) + // 纵向布局:2级表头 + InitializeVerticalLayout(centerStyle); + } + else { - dataGridView1.Columns.Add(new DataGridViewTextBoxColumn - { - Name = $"试样{i}_1", - HeaderText = $"试样{i}\n1", - DataPropertyName = $"试样{i}_1", - Width = 100, - ReadOnly = false, - DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() - }); - - dataGridView1.Columns.Add(new DataGridViewTextBoxColumn - { - Name = $"试样{i}_2", - HeaderText = $"试样{i}\n2", - DataPropertyName = $"试样{i}_2", - Width = 100, - ReadOnly = false, - DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() - }); - - dataGridView1.Columns.Add(new DataGridViewTextBoxColumn - { - Name = $"试样{i}_3", - HeaderText = $"试样{i}\n3", - DataPropertyName = $"试样{i}_3", - Width = 100, - ReadOnly = false, - DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() - }); + // 横向布局:普通表头 + InitializeHorizontalLayout(centerStyle); } // 绑定数据源 @@ -148,6 +115,125 @@ namespace WindowsFormsApp6 } } + /// + /// 初始化纵向布局(当前默认布局) + /// + private void InitializeVerticalLayout(DataGridViewCellStyle centerStyle) + { + // 序号列 + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = "序号", + HeaderText = "序号", + DataPropertyName = "序号", + Width = 180, + ReadOnly = true, + DefaultCellStyle = centerStyle + }); + + // 为每个试样添加3列(2级表头:试样N + 子列1/2/3) + for (int i = 1; i <= currentSampleCount; i++) + { + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = $"试样{i}_1", + HeaderText = $"试样{i}\n1", + DataPropertyName = $"试样{i}_1", + Width = 100, + ReadOnly = false, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = $"试样{i}_2", + HeaderText = $"试样{i}\n2", + DataPropertyName = $"试样{i}_2", + Width = 100, + ReadOnly = false, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = $"试样{i}_3", + HeaderText = $"试样{i}\n3", + DataPropertyName = $"试样{i}_3", + Width = 100, + ReadOnly = false, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + } + } + + /// + /// 初始化横向布局 + /// + private void InitializeHorizontalLayout(DataGridViewCellStyle centerStyle) + { + // 序号列 + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = "序号", + HeaderText = "序号", + DataPropertyName = "序号", + Width = 120, + ReadOnly = true, + DefaultCellStyle = centerStyle + }); + + // 数据项列 + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = ROW_WICKING_TIME, + HeaderText = ROW_WICKING_TIME, + DataPropertyName = ROW_WICKING_TIME, + Width = 120, + ReadOnly = true, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = ROW_WICKING_HEIGHT, + HeaderText = ROW_WICKING_HEIGHT, + DataPropertyName = ROW_WICKING_HEIGHT, + Width = 120, + ReadOnly = false, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = ROW_WICKING_RATE, + HeaderText = ROW_WICKING_RATE, + DataPropertyName = ROW_WICKING_RATE, + Width = 140, + ReadOnly = true, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = ROW_AVG_WICKING_RATE, + HeaderText = ROW_AVG_WICKING_RATE, + DataPropertyName = ROW_AVG_WICKING_RATE, + Width = 160, + ReadOnly = true, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + + dataGridView1.Columns.Add(new DataGridViewTextBoxColumn + { + Name = ROW_STD_DEVIATION, + HeaderText = ROW_STD_DEVIATION, + DataPropertyName = ROW_STD_DEVIATION, + Width = 120, + ReadOnly = true, + DefaultCellStyle = (DataGridViewCellStyle)centerStyle.Clone() + }); + } + /// /// 单元格编辑前事件 - 动态控制哪些单元格可以编辑 /// @@ -155,16 +241,30 @@ namespace WindowsFormsApp6 { if (e.RowIndex >= 0 && e.ColumnIndex > 0) { - string rowName = dataGridView1.Rows[e.RowIndex].Cells["序号"].Value?.ToString() ?? ""; - - // 吸芯高度(mm)行 - 所有列都可以编辑 - if (rowName == ROW_WICKING_HEIGHT) + if (isVerticalLayout) { - return; // 允许编辑 + // 纵向布局:只有吸芯高度行可以编辑 + string rowName = dataGridView1.Rows[e.RowIndex].Cells["序号"].Value?.ToString() ?? ""; + + if (rowName == ROW_WICKING_HEIGHT) + { + return; // 允许编辑 + } + + e.Cancel = true; // 其他行不可编辑 + } + else + { + // 横向布局:只有吸芯高度列可以编辑 + string columnName = dataGridView1.Columns[e.ColumnIndex].Name; + + if (columnName == ROW_WICKING_HEIGHT) + { + return; // 允许编辑 + } + + e.Cancel = true; // 其他列不可编辑 } - - // 其他行 - 取消编辑 - e.Cancel = true; } } @@ -208,10 +308,22 @@ namespace WindowsFormsApp6 { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { - string rowName = dataGridView1.Rows[e.RowIndex].Cells["序号"].Value?.ToString() ?? ""; + bool shouldRecalculate = false; - // 如果是吸芯高度行,任何列的修改都触发重新计算 - if (rowName == ROW_WICKING_HEIGHT) + if (isVerticalLayout) + { + // 纵向布局:检查是否是吸芯高度行 + string rowName = dataGridView1.Rows[e.RowIndex].Cells["序号"].Value?.ToString() ?? ""; + shouldRecalculate = (rowName == ROW_WICKING_HEIGHT); + } + else + { + // 横向布局:检查是否是吸芯高度列 + string columnName = dataGridView1.Columns[e.ColumnIndex].Name; + shouldRecalculate = (columnName == ROW_WICKING_HEIGHT); + } + + if (shouldRecalculate) { CalculateAllRows(); RefreshDataGridView(); @@ -236,6 +348,134 @@ namespace WindowsFormsApp6 }; clockTimer.Tick += (s, e) => label2.Text = DateTime.Now.ToString(DATE_TIME_FORMAT); clockTimer.Start(); + + // 动态创建切换按钮 + CreateToggleButton(); + } + + /// + /// 创建切换按钮 + /// + private void CreateToggleButton() + { + Button toggleButton = new Button + { + Name = "buttonToggle", + Text = "🔄 横向", + Width = 100, + Height = 30, + BackColor = Color.FromArgb(52, 152, 219), + ForeColor = Color.White, + FlatStyle = FlatStyle.Flat, + Font = new Font("微软雅黑", 9F, FontStyle.Bold), + Cursor = Cursors.Hand + }; + + toggleButton.FlatAppearance.BorderSize = 0; + toggleButton.Click += (s, e) => + { + ToggleTableLayout(); + toggleButton.Text = isVerticalLayout ? "🔄 横向" : "🔄 纵向"; + }; + + // 将按钮添加到 panel2 + if (this.Controls.Find("tableLayoutPanel1", true).FirstOrDefault() is TableLayoutPanel tlp1) + { + if (tlp1.Controls.Find("tableLayoutPanel2", true).FirstOrDefault() is TableLayoutPanel tlp2) + { + if (tlp2.Controls.Find("panel2", true).FirstOrDefault() is Panel panel2) + { + toggleButton.Location = new Point(panel2.Width - 120, 5); + toggleButton.Anchor = AnchorStyles.Top | AnchorStyles.Right; + panel2.Controls.Add(toggleButton); + } + } + } + } + + /// + /// 切换表格方向(纵向/横向) + /// + private void ToggleTableLayout() + { + isVerticalLayout = !isVerticalLayout; + + // 保存当前数据 + var currentData = SaveCurrentData(); + + // 重新初始化表格 + InitializeDataTable(); + InitializeDataGridView(); + + // 恢复数据 + RestoreData(currentData); + + // 更新显示 + RefreshDataGridView(); + + ShowMessage($"已切换到{(isVerticalLayout ? "纵向" : "横向")}布局", "布局切换", MessageBoxIcon.Information); + } + + /// + /// 保存当前数据 + /// + private Dictionary> SaveCurrentData() + { + var data = new Dictionary>(); + + if (sampleDataTable == null || sampleDataTable.Rows.Count == 0) + return data; + + foreach (DataRow row in sampleDataTable.Rows) + { + string rowName = row["序号"]?.ToString() ?? ""; + if (string.IsNullOrEmpty(rowName)) continue; + + var rowData = new Dictionary(); + for (int i = 1; i <= currentSampleCount; i++) + { + for (int j = 1; j <= 3; j++) + { + string colName = $"试样{i}_{j}"; + if (row.Table.Columns.Contains(colName)) + { + rowData[colName] = ConvertToDouble(row[colName]); + } + } + } + data[rowName] = rowData; + } + + return data; + } + + /// + /// 恢复数据 + /// + private void RestoreData(Dictionary> data) + { + if (data == null || data.Count == 0 || sampleDataTable == null) + return; + + foreach (DataRow row in sampleDataTable.Rows) + { + string rowName = row["序号"]?.ToString() ?? ""; + if (string.IsNullOrEmpty(rowName) || !data.ContainsKey(rowName)) + continue; + + var rowData = data[rowName]; + for (int i = 1; i <= currentSampleCount; i++) + { + for (int j = 1; j <= 3; j++) + { + string colName = $"试样{i}_{j}"; + if (row.Table.Columns.Contains(colName) && rowData.ContainsKey(colName)) + { + row[colName] = rowData[colName]; + } + } + } + } } /// @@ -323,23 +563,52 @@ namespace WindowsFormsApp6 { sampleDataTable = new DataTable(); - // 序号列 - sampleDataTable.Columns.Add("序号", typeof(string)); - - // 为每个试样添加3列(试样次数:1、2、3) - for (int i = 1; i <= currentSampleCount; i++) + if (isVerticalLayout) { - sampleDataTable.Columns.Add($"试样{i}_1", typeof(double)); - sampleDataTable.Columns.Add($"试样{i}_2", typeof(double)); - sampleDataTable.Columns.Add($"试样{i}_3", typeof(double)); - } + // 纵向布局:序号列 + 试样列 + sampleDataTable.Columns.Add("序号", typeof(string)); + + // 为每个试样添加3列(试样次数:1、2、3) + for (int i = 1; i <= currentSampleCount; i++) + { + sampleDataTable.Columns.Add($"试样{i}_1", typeof(double)); + sampleDataTable.Columns.Add($"试样{i}_2", typeof(double)); + sampleDataTable.Columns.Add($"试样{i}_3", typeof(double)); + } - // 初始化5行数据 - AddDataRow(ROW_WICKING_TIME); - AddDataRow(ROW_WICKING_HEIGHT); - AddDataRow(ROW_WICKING_RATE); - AddDataRow(ROW_AVG_WICKING_RATE); - AddDataRow(ROW_STD_DEVIATION); + // 初始化5行数据 + AddDataRow(ROW_WICKING_TIME); + AddDataRow(ROW_WICKING_HEIGHT); + AddDataRow(ROW_WICKING_RATE); + AddDataRow(ROW_AVG_WICKING_RATE); + AddDataRow(ROW_STD_DEVIATION); + } + else + { + // 横向布局:序号列 + 数据项列 + sampleDataTable.Columns.Add("序号", typeof(string)); + sampleDataTable.Columns.Add(ROW_WICKING_TIME, typeof(double)); + sampleDataTable.Columns.Add(ROW_WICKING_HEIGHT, typeof(double)); + sampleDataTable.Columns.Add(ROW_WICKING_RATE, typeof(double)); + sampleDataTable.Columns.Add(ROW_AVG_WICKING_RATE, typeof(double)); + sampleDataTable.Columns.Add(ROW_STD_DEVIATION, typeof(double)); + + // 为每个试样的每次测试添加一行 + for (int i = 1; i <= currentSampleCount; i++) + { + for (int j = 1; j <= 3; j++) + { + DataRow row = sampleDataTable.NewRow(); + row["序号"] = $"试样{i}_{j}"; + row[ROW_WICKING_TIME] = 0.0; + row[ROW_WICKING_HEIGHT] = 0.0; + row[ROW_WICKING_RATE] = 0.0; + row[ROW_AVG_WICKING_RATE] = 0.0; + row[ROW_STD_DEVIATION] = 0.0; + sampleDataTable.Rows.Add(row); + } + } + } } /// @@ -376,19 +645,41 @@ namespace WindowsFormsApp6 /// private void DataTimer_Tick(object sender, EventArgs e) { - // 第1行:读取吸水时间(s) - 从寄存器读取(每个试样读取3次) - DataRow timeRow = sampleDataTable.Rows[0]; - for (int i = 1; i <= currentSampleCount; i++) + if (isVerticalLayout) { - double time1 = ReadRegisterData((i - 1) * 3); // 第1次测试 - double time2 = ReadRegisterData((i - 1) * 3 + 1); // 第2次测试 - double time3 = ReadRegisterData((i - 1) * 3 + 2); // 第3次测试 - - timeRow[$"试样{i}_1"] = time1; - timeRow[$"试样{i}_2"] = time2; - timeRow[$"试样{i}_3"] = time3; + // 纵向布局:第1行读取吸水时间 + DataRow timeRow = sampleDataTable.Rows[0]; + for (int i = 1; i <= currentSampleCount; i++) + { + double time1 = ReadRegisterData((i - 1) * 3); + double time2 = ReadRegisterData((i - 1) * 3 + 1); + double time3 = ReadRegisterData((i - 1) * 3 + 2); + + timeRow[$"试样{i}_1"] = time1; + timeRow[$"试样{i}_2"] = time2; + timeRow[$"试样{i}_3"] = time3; + } + } + else + { + // 横向布局:每行读取吸水时间 + int rowIndex = 0; + for (int i = 1; i <= currentSampleCount; i++) + { + for (int j = 1; j <= 3; j++) + { + if (rowIndex < sampleDataTable.Rows.Count) + { + double time = ReadRegisterData((i - 1) * 3 + (j - 1)); + sampleDataTable.Rows[rowIndex][ROW_WICKING_TIME] = time; + rowIndex++; + } + } + } } + // 计算所有相关数据 + CalculateAllRows(); UpdateDisplay(); } @@ -409,35 +700,50 @@ namespace WindowsFormsApp6 // 清空所有行的数据(保持表结构) foreach (DataRow row in sampleDataTable.Rows) { - for (int i = 1; i <= currentSampleCount; i++) + for (int colIndex = 1; colIndex < sampleDataTable.Columns.Count; colIndex++) { - row[$"试样{i}_1"] = 0.0; - row[$"试样{i}_2"] = 0.0; - row[$"试样{i}_3"] = 0.0; + row[colIndex] = 0.0; } } - // 第1行:吸水时间(s)- 系统读数(每个试样3次) - DataRow timeRow = sampleDataTable.Rows[0]; - for (int i = 1; i <= currentSampleCount; i++) + if (isVerticalLayout) { - // 生成3次测试的吸水时间数据(30-34秒,略有差异) - double baseTime = 31 + random.NextDouble() * 2; // 基准时间 31-33秒 - double time1 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); // ±1秒 - double time2 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); - double time3 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); - - timeRow[$"试样{i}_1"] = time1; - timeRow[$"试样{i}_2"] = time2; - timeRow[$"试样{i}_3"] = time3; + // 纵向布局:第1行生成吸水时间 + DataRow timeRow = sampleDataTable.Rows[0]; + for (int i = 1; i <= currentSampleCount; i++) + { + double baseTime = 31 + random.NextDouble() * 2; + double time1 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); + double time2 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); + double time3 = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); + + timeRow[$"试样{i}_1"] = time1; + timeRow[$"试样{i}_2"] = time2; + timeRow[$"试样{i}_3"] = time3; + } + } + else + { + // 横向布局:每行生成吸水时间 + int rowIndex = 0; + for (int i = 1; i <= currentSampleCount; i++) + { + double baseTime = 31 + random.NextDouble() * 2; + for (int j = 1; j <= 3; j++) + { + if (rowIndex < sampleDataTable.Rows.Count) + { + double time = Math.Round(baseTime + (random.NextDouble() - 0.5) * 2, 2); + sampleDataTable.Rows[rowIndex][ROW_WICKING_TIME] = time; + rowIndex++; + } + } + } } - // 第2行:吸芯高度(mm)- 手动输入,不自动生成 - // 保持为0,等待用户手动输入 - - // 第3-5行:芯吸速率、平均值、标准偏差 - 保持为0 - // 等待用户输入吸芯高度后,会自动计算 - + // 计算所有相关数据 + CalculateAllRows(); + // 更新显示 RefreshDataGridView(); @@ -489,31 +795,42 @@ namespace WindowsFormsApp6 /// private void CalculateWickingRate() { - DataRow timeRow = sampleDataTable.Rows[0]; // 吸水时间 - DataRow heightRow = sampleDataTable.Rows[1]; // 吸芯高度 - DataRow rateRow = sampleDataTable.Rows[2]; // 芯吸速率 - - for (int i = 1; i <= currentSampleCount; i++) + if (isVerticalLayout) { - // 获取3次吸水时间测试数据 - double time1 = ConvertToDouble(timeRow[$"试样{i}_1"]); - double time2 = ConvertToDouble(timeRow[$"试样{i}_2"]); - double time3 = ConvertToDouble(timeRow[$"试样{i}_3"]); + // 纵向布局 + DataRow timeRow = sampleDataTable.Rows[0]; + DataRow heightRow = sampleDataTable.Rows[1]; + DataRow rateRow = sampleDataTable.Rows[2]; - // 获取3次吸芯高度测试数据 - double height1 = ConvertToDouble(heightRow[$"试样{i}_1"]); - double height2 = ConvertToDouble(heightRow[$"试样{i}_2"]); - double height3 = ConvertToDouble(heightRow[$"试样{i}_3"]); + for (int i = 1; i <= currentSampleCount; i++) + { + double time1 = ConvertToDouble(timeRow[$"试样{i}_1"]); + double time2 = ConvertToDouble(timeRow[$"试样{i}_2"]); + double time3 = ConvertToDouble(timeRow[$"试样{i}_3"]); - // 计算3次芯吸速率(每次测试独立计算,使用对应的时间和高度) - double rate1 = (time1 > 0 && height1 > 0) ? height1 / (time1 / 60.0) : 0; - double rate2 = (time2 > 0 && height2 > 0) ? height2 / (time2 / 60.0) : 0; - double rate3 = (time3 > 0 && height3 > 0) ? height3 / (time3 / 60.0) : 0; + double height1 = ConvertToDouble(heightRow[$"试样{i}_1"]); + double height2 = ConvertToDouble(heightRow[$"试样{i}_2"]); + double height3 = ConvertToDouble(heightRow[$"试样{i}_3"]); - // 存储到对应的列 - rateRow[$"试样{i}_1"] = Math.Round(rate1, 2); - rateRow[$"试样{i}_2"] = Math.Round(rate2, 2); - rateRow[$"试样{i}_3"] = Math.Round(rate3, 2); + double rate1 = (time1 > 0 && height1 > 0) ? height1 / (time1 / 60.0) : 0; + double rate2 = (time2 > 0 && height2 > 0) ? height2 / (time2 / 60.0) : 0; + double rate3 = (time3 > 0 && height3 > 0) ? height3 / (time3 / 60.0) : 0; + + rateRow[$"试样{i}_1"] = Math.Round(rate1, 2); + rateRow[$"试样{i}_2"] = Math.Round(rate2, 2); + rateRow[$"试样{i}_3"] = Math.Round(rate3, 2); + } + } + else + { + // 横向布局 + foreach (DataRow row in sampleDataTable.Rows) + { + double time = ConvertToDouble(row[ROW_WICKING_TIME]); + double height = ConvertToDouble(row[ROW_WICKING_HEIGHT]); + double rate = (time > 0 && height > 0) ? height / (time / 60.0) : 0; + row[ROW_WICKING_RATE] = Math.Round(rate, 2); + } } } @@ -540,25 +857,63 @@ namespace WindowsFormsApp6 /// private void CalculateAverageWickingRate() { - DataRow rateRow = sampleDataTable.Rows[2]; // 芯吸速率 - DataRow avgRow = sampleDataTable.Rows[3]; // 平均芯吸速率 - List rates = new List(); - for (int i = 1; i <= currentSampleCount; i++) + + if (isVerticalLayout) { - double rate = ConvertToDouble(rateRow[$"试样{i}_3"]); - if (rate > 0) + // 纵向布局 + DataRow rateRow = sampleDataTable.Rows[2]; + DataRow avgRow = sampleDataTable.Rows[3]; + + for (int i = 1; i <= currentSampleCount; i++) { - rates.Add(rate); + double rate1 = ConvertToDouble(rateRow[$"试样{i}_1"]); + double rate2 = ConvertToDouble(rateRow[$"试样{i}_2"]); + double rate3 = ConvertToDouble(rateRow[$"试样{i}_3"]); + + if (rate1 > 0) rates.Add(rate1); + if (rate2 > 0) rates.Add(rate2); + if (rate3 > 0) rates.Add(rate3); + } + + double average = rates.Count > 0 ? rates.Average() : 0; + + for (int i = 1; i <= currentSampleCount; i++) + { + avgRow[$"试样{i}_3"] = Math.Round(average, 2); } } - - double average = rates.Count > 0 ? rates.Average() : 0; - - // 所有试样显示相同的平均值(存储在试样次数3) - for (int i = 1; i <= currentSampleCount; i++) + else { - avgRow[$"试样{i}_3"] = Math.Round(average, 2); + // 横向布局 + foreach (DataRow row in sampleDataTable.Rows) + { + double rate = ConvertToDouble(row[ROW_WICKING_RATE]); + if (rate > 0) rates.Add(rate); + } + + double average = rates.Count > 0 ? rates.Average() : 0; + + // 只在每个试样的第3次测试显示平均值 + int rowIndex = 0; + for (int i = 1; i <= currentSampleCount; i++) + { + for (int j = 1; j <= 3; j++) + { + if (rowIndex < sampleDataTable.Rows.Count) + { + if (j == 3) + { + sampleDataTable.Rows[rowIndex][ROW_AVG_WICKING_RATE] = Math.Round(average, 2); + } + else + { + sampleDataTable.Rows[rowIndex][ROW_AVG_WICKING_RATE] = 0.0; + } + rowIndex++; + } + } + } } } @@ -567,31 +922,75 @@ namespace WindowsFormsApp6 /// private void CalculateStandardDeviation() { - DataRow rateRow = sampleDataTable.Rows[2]; // 芯吸速率 - DataRow stdRow = sampleDataTable.Rows[4]; // 标准偏差 - List rates = new List(); - for (int i = 1; i <= currentSampleCount; i++) + + if (isVerticalLayout) { - double rate = ConvertToDouble(rateRow[$"试样{i}_3"]); - if (rate > 0) + // 纵向布局 + DataRow rateRow = sampleDataTable.Rows[2]; + DataRow stdRow = sampleDataTable.Rows[4]; + + for (int i = 1; i <= currentSampleCount; i++) { - rates.Add(rate); + double rate1 = ConvertToDouble(rateRow[$"试样{i}_1"]); + double rate2 = ConvertToDouble(rateRow[$"试样{i}_2"]); + double rate3 = ConvertToDouble(rateRow[$"试样{i}_3"]); + + if (rate1 > 0) rates.Add(rate1); + if (rate2 > 0) rates.Add(rate2); + if (rate3 > 0) rates.Add(rate3); + } + + double stdDev = 0; + if (rates.Count > 1) + { + double average = rates.Average(); + double sumOfSquares = rates.Sum(r => Math.Pow(r - average, 2)); + stdDev = Math.Sqrt(sumOfSquares / (rates.Count - 1)); + } + + for (int i = 1; i <= currentSampleCount; i++) + { + stdRow[$"试样{i}_3"] = Math.Round(stdDev, 2); } } - - double stdDev = 0; - if (rates.Count > 1) + else { - double average = rates.Average(); - double sumOfSquares = rates.Sum(r => Math.Pow(r - average, 2)); - stdDev = Math.Sqrt(sumOfSquares / (rates.Count - 1)); - } + // 横向布局 + foreach (DataRow row in sampleDataTable.Rows) + { + double rate = ConvertToDouble(row[ROW_WICKING_RATE]); + if (rate > 0) rates.Add(rate); + } - // 所有试样显示相同的标准偏差(存储在试样次数3) - for (int i = 1; i <= currentSampleCount; i++) - { - stdRow[$"试样{i}_3"] = Math.Round(stdDev, 2); + double stdDev = 0; + if (rates.Count > 1) + { + double average = rates.Average(); + double sumOfSquares = rates.Sum(r => Math.Pow(r - average, 2)); + stdDev = Math.Sqrt(sumOfSquares / (rates.Count - 1)); + } + + // 只在每个试样的第3次测试显示标准偏差 + int rowIndex = 0; + for (int i = 1; i <= currentSampleCount; i++) + { + for (int j = 1; j <= 3; j++) + { + if (rowIndex < sampleDataTable.Rows.Count) + { + if (j == 3) + { + sampleDataTable.Rows[rowIndex][ROW_STD_DEVIATION] = Math.Round(stdDev, 2); + } + else + { + sampleDataTable.Rows[rowIndex][ROW_STD_DEVIATION] = 0.0; + } + rowIndex++; + } + } + } } }