using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Linq; using System.Windows.Forms; using System.IO; using DevExpress.XtraWaitForm; using DevExpress.XtraSplashScreen; using DevExpress.XtraCharts; using System.Data.SQLite; using LFP_Manager.DataStructure; using LFP_Manager.Function; using LFP_Manager.Utils; namespace LFP_Manager.Controls { public partial class ucHistroy : DevExpress.XtraEditors.XtraUserControl { #region VARIABLES CommConfig Config; DataTable dtHistory; #endregion #region CONSTRUCTOR public ucHistroy() { InitializeComponent(); dtHistory = new DataTable(); } public void SetCommCofig(CommConfig aConfig) { Config = aConfig; } #endregion #region SQL QUARY EXCUTE private void quaryExcute(string quary) { string dbFilename = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + DbConstData.Database.FileName; // Open database string strConn = @"Data Source=" + dbFilename; using (var connection = new SQLiteConnection(strConn)) { connection.Open(); try { DataTable aT = new DataTable(); DbWaitForm.ShowWaitForm(); csDbUtils.BeginTran(connection); // Update data using (SQLiteCommand command = connection.CreateCommand()) { //sSQL = "insert into TrendTable ( TrendStamp, TagName, TagValue) Values ( " + IntToStr(stamp) + "," + name + "," + value + ");"; command.CommandText = quary; try { SQLiteDataReader reader = command.ExecuteReader(); aT.Load(reader); } catch (Exception) { } } csDbUtils.CommitTran(connection); dtHistory = aT; //RealGridView.DataSource = dt; DbWaitForm.CloseWaitForm(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { connection.Close(); } } } #endregion #region BUTTON EVENT private void btnSearch_Click(object sender, EventArgs e) { DateTime start = dtpStart.Value; DateTime mid; DateTime end = dtpEnd.Value; TimeSpan span = end - start; int sDay = start.DayOfYear; int eDay = end.DayOfYear; dtHistory.Clear(); int total_days = eDay - sDay; DataTable[] dDt = new DataTable[total_days + 1]; string quary = ""; try { if (total_days == 0) { quary = String.Format(" where create_date > '{0:yyyy-MM-dd HH:mm:ss}'", start); quary += String.Format(" and create_date < '{0:yyyy-MM-dd HH:mm:ss}'", end); dDt[0] = csDbUtils.BmsDataSelectToDataTable(Config, start, quary); if (dDt[0] == null) MessageBox.Show("No data", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); else { dtHistory = dDt[0]; dtHistory.TableName = "THistory"; gridSearchResult.DataSource = dtHistory; //gridSearchResult.(); } } else { DataTable bDt = new DataTable(); for (int i = 0; i < (total_days + 1); i++) { //dDt[i] = new DataTable(); if (i == 0) { quary = String.Format(" where create_date > '{0:yyyy-MM-dd HH:mm:ss}'", start); dDt[i] = csDbUtils.BmsDataSelectToDataTable(Config, start, quary); } else if (i == total_days) { quary = String.Format(" where create_date < '{0:yyyy-MM-dd HH:mm:ss}'", end); dDt[i] = csDbUtils.BmsDataSelectToDataTable(Config, end, quary); } else { quary = ""; mid = start.AddDays(i); dDt[i] = csDbUtils.BmsDataSelectToDataTable(Config, mid, quary); } if (dDt[i] != null) bDt.Merge(dDt[i]); } if (bDt == null) MessageBox.Show("No data", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); else { dtHistory = bDt; dtHistory.TableName = String.Format("THistory_{0:yyMMdd_HHmmss}", DateTime.Now); gridSearchResult.DataSource = dtHistory; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnExportExcel_Click(object sender, EventArgs e) { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Title = "Select save file"; sDialog.DefaultExt = "xlsx"; sDialog.Filter = "Excel files 2003 (*.xls)|*.xls|All files (*.*)|*.*"; if (sDialog.ShowDialog() == DialogResult.OK) { string filename = sDialog.FileName; try { csExcelExport.ExportToExcelExt(dtHistory, filename); MessageBox.Show("Complete Export Excel File", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } #endregion #region COMPONENT EVENT private void gridSearchResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.Value is DateTime dateTime) { e.Value = csUtils.FormatDateTime(dateTime); e.FormattingApplied = true; } } #endregion } }