C3032
2026-01-16 919a8efe5f75b9d84ed79c91dfbea5263da8ce59
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using LB_SmartVisionCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace LB_SmartVision.Forms.Pages.HistoricalData
{
    public partial class HistoricalDataEditPage : UserControl
    {
        public Action<string, LogInfoType> LogInfo;
 
        private int row2OriginalHeight;
        private int row3OriginalHeight;
        public HistoricalDataEditPage()
        {
            Name = "HistoricalDataEditPage";
            Text = "历史数据查询";
 
            InitializeComponent();
 
            // 保存原始行高度
            row2OriginalHeight = (int)tableLayoutPanel3.RowStyles[1].Height;
            row3OriginalHeight = (int)tableLayoutPanel3.RowStyles[2].Height;
 
            InitializeDataGridView();
            InitializeComboBox();
        }
 
        /// <summary>
        /// 数据显示表格初始化
        /// </summary>
        private void InitializeDataGridView()
        {
            // 设置DataGridView列宽
            dataGridViewHD.ColumnCount = 4;
 
            int totalWidth = dataGridViewHD.ClientSize.Width;
            int columnCount = dataGridViewHD.ColumnCount;
            int columnWidth = totalWidth / columnCount;
 
            // 设置最小宽度
            int minWidth = 100; // 最小宽度
            if (columnWidth < minWidth)
            {
                columnWidth = minWidth;
            }
 
            for (int i = 0; i < columnCount; i++)
            {
                dataGridViewHD.Columns[i].Width = columnWidth;
            }
 
            // 设置列标题
            dataGridViewHD.Columns[0].Name = "SN号";
            dataGridViewHD.Columns[1].Name = "时间";
            dataGridViewHD.Columns[2].Name = "NG类";
            dataGridViewHD.Columns[3].Name = "缺陷大小";
 
            dataGridViewHD.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
 
            // 禁止编辑单元格(可选)
            dataGridViewHD.ReadOnly = true;
 
            // 允许多行选择(可选)
            dataGridViewHD.MultiSelect = false;
 
            // 显示行标题(可选)
            dataGridViewHD.RowHeadersVisible = true;
        }
 
        /// <summary>
        /// 选择依据下拉框初始化
        /// </summary>
        private void InitializeComboBox()
        {
            comboBoxSearchBasis.Items.Add("日期");
            comboBoxSearchBasis.Items.Add("SN号");
 
            // 设置默认选择项
            comboBoxSearchBasis.SelectedIndex = 0;
        }
 
        /// <summary>
        /// 下拉框控制选项视图变换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxSearchBasis_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;
 
            if (cb.SelectedIndex == 0)
            {
                ShowRow2();
            }
            else if (cb.SelectedIndex == 1)
            {
                ShowRow3();
            }
        }
 
        private void ShowRow2()
        {
            // 显示Row2(恢复高度)
            tableLayoutPanel3.RowStyles[1].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[1].Height = row2OriginalHeight;
 
            // 隐藏Row3(高度设为0)
            tableLayoutPanel3.RowStyles[2].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[2].Height = 0;
 
            // 显示Row2中的控件
            foreach (Control ctrl in tableLayoutPanel3.Controls)
            {
                int row = tableLayoutPanel3.GetRow(ctrl);
                if (row == 1)
                    ctrl.Visible = true;
                else if (row == 2)
                    ctrl.Visible = false;
            }
        }
 
        private void ShowRow3()
        {
            // 隐藏Row2(高度设为0)
            tableLayoutPanel3.RowStyles[1].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[1].Height = 0;
 
            // 显示Row3(恢复高度)
            tableLayoutPanel3.RowStyles[2].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[2].Height = row3OriginalHeight;
 
            // 显示Row3中的控件
            foreach (Control ctrl in tableLayoutPanel3.Controls)
            {
                int row = tableLayoutPanel3.GetRow(ctrl);
                if (row == 1)
                    ctrl.Visible = false;
                else if (row == 2)
                    ctrl.Visible = true;
            }
        }
 
        private void dateTimePickerStart_ValueChanged(object sender, EventArgs e)
        {
            dateTimePickerEnd.MinDate = dateTimePickerStart.Value;
        }
 
        private void btnHisDataExport_Click(object sender, EventArgs e)
        {
 
        }
 
        public void SaveToCSV(string filePath, int rowIndex)
        {
            // 使用 StreamWriter 来写入文件
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                DataGridViewRow row = dataGridViewHD.Rows[rowIndex];
 
                int rowCount = 10;//10改为采集次数
 
                writer.WriteLine("SN号, 时间, NG类, 缺陷大小");
 
                // 遍历每一行数据
                for (int i = 0; i < rowCount; i++)
                {
                    int batchSize = 4;
 
                    // 写入这一批次的每一列数据
                    for (int k = 0; k < batchSize; k++)
                    {
                        writer.Write($"{row.Cells[k].Value}");
 
                        // 在元素之间添加逗号,除非这是最后一个元素
                        if (k < batchSize - 1)
                        {
                            writer.Write(",");
                        }
                    }
 
                    // 写入完一行后,换行
                    writer.WriteLine();
                    
                }
            }
        }
    }
}