using LB_SmartVision.CSV;
|
using LB_SmartVision.SQL;
|
using LB_SmartVisionCommon;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Globalization;
|
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 List<DataGridViewRow> originalRows = new List<DataGridViewRow>();
|
|
private int row2OriginalHeight;
|
private int row3OriginalHeight;
|
|
List<RecordProductData> RecordProductDatas = new List<RecordProductData>();
|
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()
|
{
|
this.dataGridViewHD.DataSource = RecordProductDatas;
|
|
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;
|
}
|
public string OpenFileForSave()
|
{
|
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
|
{
|
// 设置默认文件名和扩展名
|
saveFileDialog.FileName = $"历史数据_{DateTime.Now:yyyyMMdd_HHmmss}";
|
saveFileDialog.DefaultExt = "xlsx";
|
|
// 设置文件过滤器
|
saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx|" +
|
"CSV文件 (*.csv)|*.csv|" +
|
"文本文件 (*.txt)|*.txt|" +
|
"所有文件 (*.*)|*.*";
|
|
saveFileDialog.Title = "选择文件保存位置";
|
saveFileDialog.RestoreDirectory = true; // 记住上次打开的目录
|
saveFileDialog.OverwritePrompt = true; // 覆盖时提示
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
string filePathHD = saveFileDialog.FileName;
|
return filePathHD;
|
}
|
}
|
|
return null;
|
}
|
private void btnHisDataExport_Click(object sender, EventArgs e)
|
{
|
CsvDataHelperRecordProductData.SaveToCsv(OpenFileForSave(), RecordProductDatas);
|
}
|
|
private void btnHisDataFind_Click(object sender, EventArgs e)
|
{
|
|
DateTime startDate = dateTimePickerStart.Value.Date;
|
DateTime endDate = dateTimePickerEnd.Value.Date;
|
|
switch(comboBoxSearchBasis.SelectedIndex)
|
{
|
//依据日期查询
|
case 0:
|
RecordProductDatas = RecordProductDataRepository.GetRecordsByTimeRange(DateTime.Parse(this.dateTimePickerStart.Value.ToString()), DateTime.Parse(this.dateTimePickerEnd.Value.ToString()));
|
break;
|
//依据SN号查询
|
case 1:
|
if(string.IsNullOrEmpty(textBoxSN.Text))
|
{
|
MessageBox.Show("请填写SN号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
}
|
else
|
{
|
RecordProductDatas = RecordProductDataRepository.GetRecordsByProductNumber(textBoxSN.Text);
|
}
|
break;
|
}
|
|
this.dataGridViewHD.DataSource = RecordProductDatas;
|
this.dataGridViewHD.AutoGenerateColumns = true;
|
|
}
|
|
}
|
}
|