DataGridView歷史記錄的前進與後退操作
阿新 • • 發佈:2019-02-19
此功能介紹:DataGridView頻繁查詢資料時,可以將每次查詢到的資料結果儲存到.xml檔案中。並給使用者提供前進、後退按鈕操作.當用戶想檢視上一次的查詢結果時,可直接從儲存到的.xml檔案中載入資料,提高查詢速度。
/// <summary>
/// DataGridView所在窗體功能前進與後退
///檔案格式: File$001.xml File$002.xml
///當前檔案: File$003Cur.xml
///使用方法:
/// 定義全域性窗體級變數(以便各模組呼叫)
/// HR.Controls.DataGridViewOperation.FunctionForwardBack FFB = new HR.Controls.DataGridViewOperation.FunctionForwardBack();
///
/// 在窗體啟動時執行(以便刪除上次未刪除的臨時儲存檔案)
/// FFB.DeleteXmlFile(功能模組ID);
///
/// 當資料來源DataSource改變時
/// FFB.SaveXmlFile(功能模組ID, DataGridView控制元件名, 前進控制元件, 後退控制元件);
///
/// 在窗體退出時執行(以便刪除臨時儲存檔案)
/// BC.XmlFileDelete(功能模組ID)
///
/// 在點選“前進”時執行
/// FFB.DataGridViewSetData(功能模組ID, DataGridView控制元件名, HR.Controls.DataGridViewOperation.FunctionForwardBack.ForwardBackOption.Forward, 前進控制元件, 後退控制元件);
///
/// 在點選“後退”時執行
/// FFB.DataGridViewSetData(功能模組ID, DataGridView控制元件名, HR.Controls.DataGridViewOperation.FunctionForwardBack.ForwardBackOption.Back, 前進控制元件, 後退控制元件);
/// </summary>
public class FunctionForwardBack
{
#region DataGridView歷史資料的前進、後退操作
/// <summary>
/// 前進後退操作
/// </summary>
/// <remarks></remarks>
public enum ForwardBackward
{
/// <summary>
/// 前進
/// </summary>
Forward,
/// <summary>
/// 後退
/// </summary>
Backward,
}
/// <summary>
/// DataGridView控制元件前進或後退
/// </summary>
/// <param name="FormID">功能編號</param>
/// <param name="Dg">DataGridView控制元件</param>
/// <param name="ForwardBack">前進或後退</param>
/// <param name="ForwardControl">前進控制元件</param>
/// <param name="BackwardControl">後退控制元件</param>
/// <returns></returns>
public bool SetDataGridViewData(string FormID, DataGridView Dg, ForwardBackward ForwardBack, object ForwardControl, object BackwardControl)
{
string CurDir = System.Windows.Forms.Application.StartupPath + "\\TempFile\\" + FormID + "\\";
if (!Directory.Exists(CurDir))
return false;
if (this.GetFilesInCurrentDir(CurDir,"*.xml").Count<=0) //要查詢的檔案不存在
{
return false;
}
DataSet ds = new DataSet();
try
{
ds.ReadXml(RestoreAllFile(FormID, ForwardBack, ForwardControl, BackwardControl));
Dg.DataSource = null;
if (ds.Tables.Count>0)
{
Dg.DataSource = ds.Tables[0];
}
SetContrl(FormID, ForwardControl, BackwardControl);//資料繫結完後,重新設定一下前進、後退按鈕
return true;
}
catch
{
SetContrl(FormID, ForwardControl, BackwardControl);
return false;
}
return true;
}
/// <summary>
/// 將檔名全部重置,並返回當前檔案
/// </summary>
/// <param name="FormID">功能編號</param>
/// <param name="ForwardBack">前進或後退</param>
/// <param name="ForwardControl">前進控制元件</param>
/// <param name="BackwardControl">後退控制元件</param>
/// <returns>當前檔案</returns>
private string RestoreAllFile(string FormID, ForwardBackward ForwardBack, object ForwardControl, object BackwardControl)
{
int CurrentIndex = RestoreFileName(FormID, ForwardBack, ForwardControl, BackwardControl);
if (CurrentIndex == 9999)
return null;
string CurDir = System.Windows.Forms.Application.StartupPath + "\\TempFile\\" + FormID + "\\";
ArrayList FileArry = this.GetFilesInCurrentDir(CurDir, "*.xml");
string SourceFile = string.Empty;
string DestFile = string.Empty;
switch (ForwardBack)
{
case ForwardBackward.Forward: //如果是前進
CurrentIndex++; //前當檔案的索引加1
SourceFile =CurDir + FileArry[CurrentIndex].ToString();
if (File.Exists(SourceFile))
{
DestFile=FileArry[CurrentIndex].ToString().Replace("File$","").Substring(0,3).Trim();
DestFile =CurDir + "File$" + DestFile.PadLeft(3,'0') + "Cur" + ".xml";
File.Copy(SourceFile, DestFile, true);
System.Windows.Forms.Application.DoEvents();
File.Delete(SourceFile);
}
break;
case ForwardBackward.Backward://如果是後退
CurrentIndex--;
SourceFile = CurDir + FileArry[CurrentIndex].ToString();
if (File.Exists(SourceFile))
{
DestFile = FileArry[CurrentIndex].ToString().Replace("File$", "").Substring(0, 3).Trim();
DestFile = CurDir + "File$" + DestFile.PadLeft(3, '0') + "Cur" + ".xml";
File.Copy(SourceFile, DestFile, true);
System.Windows.Forms.Application.DoEvents();
File.Delete(SourceFile);
}
break;
}
return DestFile;
}
/// <summary>
/// 將檔名全部重置,並返回當前檔案索引
/// </summary>
/// <param name="FormID">功能編號</param>
/// <param name="ForwardBack">前進或後退</param>
/// <param name="ForwardControl">前進控制元件</param>
/// <param name="BackwardControl">後退控制元件</param>
/// <returns>當前檔案索引</returns>
private int RestoreFileName(string FormID, ForwardBackward ForwardBack, object ForwardControl, object BackwardControl)
{
string CurDir = System.Windows.Forms.Application.StartupPath + "\\TempFile\\" + FormID + "\\";
ArrayList FileArry=this.GetFilesInCurrentDir(CurDir,"*.xml");
int CurrIndex = FileArry.Count - 1; //檔案索引數
string SourceFile = string.Empty;
string DestFile = string.Empty;
for (int i = 0; i < FileArry.Count; i++)
{
if (FileArry[i].ToString().Length > 12) //檔案列表中有File$xxxCur.xml的檔案存在
{
if (i==0 && ForwardBack==ForwardBackward.Backward)
{
return 9999;
}
if(i==FileArry.Count-1 && ForwardBack==ForwardBackward.Forward)
{
return 9999;
}
try
{
CurrIndex = i;
SourceFile = CurDir + FileArry[i].ToString();
DestFile = FileArry[i].ToString().Replace("File$", "").Substring(0, 3).Trim();
DestFile = CurDir + "File$" + DestFile.PadLeft(3, '0') + ".xml";
File.Copy(SourceFile, DestFile);
System.Windows.Forms.Application.DoEvents();
File.Delete(SourceFile);
}
catch
{
return 9999;
}
}
}
return CurrIndex;
}
#endregion
#region DataGridView 每次查詢時後將結果儲存為xml檔案
/// <summary>
/// 儲存資料結果到Xml檔案,在資料結果變化前
/// </summary>
/// <param name="FormID">功能編號</param>
/// <param name="DG">要儲存的DataGridView控制元件</param>
/// <param name="ForwardControl">前進控制元件</param>
/// <param name="BackControl">後退控制元件</param>
/// <returns></returns>
public bool SaveXmlFile(string FormID, DataGridView DG, object ForwardControl, object BackControl)
{
if (DG.DataSource == null)
return false;
string CurDir = System.Windows.Forms.Application.StartupPath + "\\TempFile\\" + FormID + "\\";
DirectoryInfo TempDir = new DirectoryInfo(CurDir);
int MaxID = 1;
if (!TempDir.Exists)
TempDir.Create();
else
MaxID = RenFileAll(FormID);
string NewFileName = "File$" + MaxID.ToString().PadLeft(3, '0') + ".xml";
System.Data.DataTable dt = (System.Data.DataTable)DG.DataSource;
dt.TableName = "HR";
dt.WriteXml(CurDir+NewFileName, XmlWriteMode.WriteSchema);
System.Windows.Forms.Application.DoEvents();
GC.Collect();
SetContrl(FormID, ForwardControl, BackControl);
return true;
}
/// <summary>
/// 重置檔案列表序列
/// </summary>
/// <param name="FormID">功能編號</param>
/// <returns></returns>
private int RenFileAll(string FormID)
{
string CurDir = System.Windows.Forms.Application.StartupPath + "\\TempFile\\" + FormID + "\\";
ArrayList fileArry = null;
fileArry = GetFilesInCurrentDir(CurDir, "*.xml");
int i=0;
int CurIndex = 0;
bool isDelFile=false;
for (i =0; i< fileArry.Count; i++)
{
if (fileArry[i].ToString().Length>12) //檔案是File$00xCur.xml形式的
{
isDelFile = true;
CurIndex = i + 1;
}
}
if (isDelFile) //刪除[File$00xCur.xml]這個檔案後面的所有檔案
{
for ( i=CurIndex ; i < fileArry.Count; i++)
{
string SourFile = fileArry[i].ToString();
try
{
if (File.Exists(SourFile))
File.Delete(SourFile);
}
catch
{
}
}
}
fileArry.Clear();
fileArry = GetFilesInCurrentDir(CurDir, "*.xml");
for (i = 0; i < fileArry.Count; i++)
{
if (fileArry[i].ToString().Length>12)
{
string SourFile = null;
string DestFile = null;
SourFile = CurDir + fileArry[i].ToString();
if (File.Exists(SourFile))
{
DestFile = int.Parse(fileArry[i].ToString().Replace("File$", "").Substring(0, 3)).ToString().Trim();
DestFile = CurDir + "File$" + DestFile.PadLeft(3, '0') + ".xml";
File.Copy(SourFile, DestFile);
File.Delete(SourFile);
}
}
}
return fileArry.Count + 1;
}
#endregion
#region "查詢檔案在當前目錄,不包括子目錄"
/// <summary>
/// 查詢檔案在當前目錄,不包括子目錄
/// </summary>
/// <param name="DirPath">要查詢的資料夾.</param>
/// <param name="FileExtName">副檔名</param>
/// <returns></returns>
private ArrayList GetFilesInCurrentDir(string DirPath, string FileExtName)
{
System.Collections.ArrayList FileArry = new ArrayList();
if (!string.IsNullOrEmpty(DirPath))
{
DirectoryInfo RootDir = new DirectoryInfo(DirPath);
if (RootDir.Exists)
{
try
{
foreach (FileInfo file in RootDir.GetFiles(FileExtName))
{
FileArry.Add(file.Name);
}
}
catch
{
}
}
}
// FileArry.Sort();
return FileArry;
}
#endregion
#region 前進、後退按鈕的控制
/// <summary>
/// 設定前進、後退控制的可用或不可用
/// </summary>
/// <param name="FormID"></param>
/// <param name="ForewardContrl"></param>
/// <param name="BackwardContrl"></param>
private void SetContrl(string FormID, object ForewardContrl, object BackwardContrl)
{
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
SetControlEnable(true, true, ForewardContrl, BackwardContrl);
string CurDir = System.Windows.Forms.Application.StartupPath + "\\TempFile\\" + FormID + "\\";
DirectoryInfo TempDir = new DirectoryInfo(CurDir);
if (!TempDir.Exists)
{
SetControlEnable(false, false,ForewardContrl,BackwardContrl);
return;
}
ArrayList FileList = this.GetFilesInCurrentDir(CurDir, "*.xml");
if (FileList.Count < 2)
{
SetControlEnable(false, false, ForewardContrl, BackwardContrl);
}
else
{
for (int i = 0; i < FileList.Count; i++)
{
if (FileList[i].ToString().Length>12)
{
if (i == 0)
SetControlEnable(true, false,ForewardContrl,BackwardContrl);
if (i == FileList.Count - 1)
SetControlEnable(false, true, ForewardContrl, BackwardContrl);
}
}
}
return;
}
/// <summary>
/// 設定前後、後退按鈕的可用狀態
/// </summary>
/// <param name="isForewardContrl">if set to <c>true</c> [is foreward contrl].</param>
/// <param name="isBackwardContrl">if set to <c>true</c> [is backward contrl].</param>
/// <param name="ForewardContrl">The foreward contrl.</param>
/// <param name="BackwardContrl">The backward contrl.</param>
/// <remarks>前後、後退按鈕分三種類型[System.Windows.Forms.ToolStripButton],[System.Windows.Forms.Button],[System.Windows.Forms.toolStripMenuItem]</remarks>
private void SetControlEnable(bool isForewardContrl, bool isBackwardContrl, object ForewardContrl, object BackwardContrl)
{
if (ForewardContrl != null)
{
if (ForewardContrl.GetType().ToString() == "System.Windows.Forms.ToolStripButton")
{
((ToolStripButton)ForewardContrl).Enabled = isForewardContrl;
}
else if (ForewardContrl.GetType().ToString() == "System.Windows.Forms.Button")
{
((System.Windows.Forms.Button)ForewardContrl).Enabled = isForewardContrl;
}
else if (ForewardContrl.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem")
{
((System.Windows.Forms.ToolStripMenuItem)ForewardContrl).Enabled = isForewardContrl;
}
}
if (BackwardContrl != null)
{
if (BackwardContrl.GetType().ToString() == "System.Windows.Forms.ToolStripButton")
{
((System.Windows.Forms.ToolStripButton)BackwardContrl).Enabled = isBackwardContrl;
}
else if (BackwardContrl.GetType().ToString() == "System.Windows.Forms.Button")
{
((System.Windows.Forms.Button)BackwardContrl).Enabled = isBackwardContrl;
}
else if (BackwardContrl.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem")
{
((System.Windows.Forms.ToolStripMenuItem)BackwardContrl).Enabled = isBackwardContrl;
}
}
}
#endregion
/// <summary>
/// 窗體關閉或啟動時需要刪除檔案
/// </summary>
/// <param name="FormID">窗體ID編號</param>
/// <returns></returns>
public bool DeleteXmlFile(string FormID)
{
bool issuccess = true;
string CurDir = System.Windows.Forms.Application.StartupPath + "\\TempFile\\" + FormID + "\\";
if (!Directory.Exists(CurDir))
return issuccess= false;
ArrayList FileArry = this.GetFilesInCurrentDir(CurDir, "*.xml");
if (FileArry.Count > 1)
{
for (int i = 0; i < FileArry.Count; i++)
{
try
{
File.Delete(CurDir + FileArry[i].ToString());
issuccess= true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
issuccess= false;
}
}
}
return issuccess;
}
}