using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.Data;
using System.Threading;
namespace 模板1
{
public partial class FormCode
{
// 启用浏览器功能的表单不支持成员变量。
// 请使用代码从 FormState 词典
// 写入和读取这些值,如下所示:
//
//private int RowCount
//{
// get
// {
// return Convert.ToInt16(Rows);
// }
// set
// {
// Rows = value;
// }
//}
// 注意: 以下是 Microsoft Office InfoPath 所需的过程。
// 可以使用 Microsoft Office InfoPath 对其进行修改。
public void InternalStartup()
{
((ButtonEvent)EventManager.ControlEvents["CTRL5_5"]).Clicked += new ClickedEventHandler(CTRL5_5_Clicked);
((ButtonEvent)EventManager.ControlEvents["CTRL6_5"]).Clicked += new ClickedEventHandler(CTRL6_5_Clicked);
EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
((ButtonEvent)EventManager.ControlEvents["CTRL10_5"]).Clicked += new ClickedEventHandler(CTRL10_5_Clicked);
((ButtonEvent)EventManager.ControlEvents["CTRL11_5"]).Clicked += new ClickedEventHandler(CTRL11_5_Clicked);
}
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
TempTable = GenDataTable(out Rows);
navRows = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:rows", NamespaceManager);
navPages = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:pages", NamespaceManager);
navCurrentPage = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:currentPage", NamespaceManager);
PageIndex = 1;
Page();
}
//重复表的父节点
string StrExpression = "/my:myFields/my:group1";
//重复表当前节点
string Node = "my:group2";
//需要导入的域
string[] Field ={ "my:field1", "my:field2", "my:field3" };
//当前页
int PageIndex = 1;
//每页10条
int RowsPrePage = 10;
//页数
int Pages = 0;
//记录数
int Rows=0;
//临时表
int LastRows = 0;
int LoopTimes = 0;
DataTable TempTable = null;
XPathNavigator navRows = null;
XPathNavigator navPages = null;
XPathNavigator navCurrentPage = null;
/// <summary>
/// 执行分页
/// </summary>
public void Page()
{
XPathNavigator nav = this.MainDataSource.CreateNavigator().SelectSingleNode(StrExpression, NamespaceManager);
DataTable2Rpt(StrExpression, Node, TempTable, Field, PageIndex);
navRows.SetValue(Rows.ToString());
navCurrentPage.SetValue(PageIndex.ToString());
navPages.SetValue(Pages.ToString());
}
public void CTRL10_5_Clicked(object sender, ClickedEventArgs e)
{
PageIndex = 1;
Page();
}
public void CTRL11_5_Clicked(object sender, ClickedEventArgs e)
{
if (PageIndex < Pages)
{
PageIndex = Pages;
Page();
}
}
public void CTRL5_5_Clicked(object sender, ClickedEventArgs e)
{
if (PageIndex <=1)
{
PageIndex = 1;
return;
}
else
{
PageIndex--;
Page();
}
}
public void CTRL6_5_Clicked(object sender, ClickedEventArgs e)
{
if (PageIndex >= Pages)
{
PageIndex = Pages;
return;
}
else
{
PageIndex++;
Page();
}
}
/// <summary>
/// 生成演示数据
/// </summary>
/// <param name="rows">输出参数rows</param>
/// <returns>DataTable</returns>
public DataTable GenDataTable(out int rows)
{
DataTable dt = new DataTable();
for (int i = 0; i < 3; i++)
{
dt.Columns.Add(i.GetHashCode().ToString());
}
for (int i = 1; i < 300; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < 3; j++)
{
dr[j] = i.ToString() +"--"+ j.ToString();
}
dt.Rows.Add(dr);
}
rows = dt.Rows.Count;
return dt;
}
public XPathNavigator GetLastChild(XPathNavigator list)
{
XPathNodeIterator iter = list.SelectChildren(XPathNodeType.Element);
XPathNavigator nav = null;
while (iter.MoveNext())
{
nav = iter.Current;
}
return nav;
}
public void DelFirstChild(XPathNavigator list)
{
XPathNodeIterator iter = list.SelectChildren(XPathNodeType.Element);
while (iter.MoveNext())
{
if (iter.CurrentPosition == 1)
{
iter.Current.DeleteSelf();
}
}
}
public void DeleteAll(string body, string node)
{
XPathNavigator parent = this.MainDataSource.CreateNavigator().SelectSingleNode(body, this.NamespaceManager);
if (parent != null)
{
XPathNodeIterator rowitor = parent.Select(node, this.NamespaceManager);
if (rowitor.Count > 0)
{
for (int i = 0; i < Convert.ToInt32(rowitor.Count) - 1; i++)
{
rowitor.Current.DeleteSelf();
rowitor.MoveNext();
}
}
}
}
public int GetCount(string body, string node)
{
XPathNavigator parent = this.MainDataSource.CreateNavigator().SelectSingleNode(body, this.NamespaceManager);
if (parent != null)
{
XPathNodeIterator rowitor = parent.Select(node, this.NamespaceManager);
return rowitor.Count;
}
else
{
return 0;
}
}
public void DelRange(string body, string node,int count)
{
try
{
XPathNavigator parent = this.MainDataSource.CreateNavigator().SelectSingleNode(body, this.NamespaceManager);
if (parent != null)
{
XPathNodeIterator rowitor = parent.Select(node, this.NamespaceManager);
XPathNavigator xn = this.MainDataSource.CreateNavigator();
XPathNavigator n1 = xn.SelectSingleNode(body + "/" + node + "[1]", this.NamespaceManager);
XPathNavigator n2 = n1.SelectSingleNode(body + "/" + node + "[" + count + "]", this.NamespaceManager);
xn.MoveTo(n1);
if (rowitor.Count > 1)
{
xn.DeleteRange(n2);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将DataTable导入进重复表
/// </summary>
/// <param name="strExpression">重复表的父节点</param>
/// <param name="dt">传入的DataTable</param>
/// <param name="fields">域名的数组集合</param>
public void DataTable2Rpt(string strExpression, string node, DataTable dt, string[] fields, int index)
{
if (Rows % RowsPrePage == 0)
{
Pages = Rows / RowsPrePage;
}
else
{
Pages = Convert.ToInt32(Rows / RowsPrePage + 1);
}
if (index > Pages)
{ return; }
XPathNavigator Nav = this.MainDataSource.CreateNavigator().SelectSingleNode(strExpression, NamespaceManager);
XPathNodeIterator iter = Nav.SelectChildren(XPathNodeType.Element);
XPathNodeIterator newNodeIter = null;
XPathNavigator newNode = null;
//复制一条
while (iter.MoveNext())
{
if (iter.CurrentPosition == 1)
{
newNode = iter.Current.Clone();
}
}
int thisrows = 0;
int beginRow = 0;
if (index != Pages)
{
thisrows=RowsPrePage * index;
beginRow=thisrows - RowsPrePage ;
}
else
{
beginRow = RowsPrePage * (index-1);
thisrows = Rows;
}
LastRows=thisrows-beginRow;
//thisrows = RowsPrePage * index;
if (thisrows <= dt.Rows.Count && thisrows > 0)
{
for (int i = beginRow; i < thisrows; i++)
{
Nav.AppendChild(newNode);
newNode = GetLastChild(Nav);
newNodeIter = newNode.SelectChildren(XPathNodeType.Element);
for (int j = 0; j < dt.Columns.Count; j++)
{
//导入将对应位置的值
newNodeIter.Current.SelectSingleNode(fields[j], NamespaceManager).SetValue(dt.Rows[i][j].ToString());
}
}
}
//删除第一条无效数据
if (LoopTimes == 0)
{
DelFirstChild(Nav);
LoopTimes++;
}
else //删除之前的数据
{
LoopTimes++;
DelRange(StrExpression, node, LastRows);
}
}
}
}