此功能介绍: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;
}
}