InfoPath中重复表的操作+repeat+table+operation

 loop through items in a repeating table in InfoPath 2007

 

http://www.bizsupportonline.net/infopath2007/loop-through-items-repeating-table.htm

You can accomplish this functionality as follows:

  1. Open Microsoft Office InfoPath 2007, create a new blank form, and add a Repeating Table and a Button control to the form template.
  2. In the Data Source pane, rename group1 to table and group2 to row.
    The main data source of your form template should resemble the following figure:


    Figure 1. The main data source of the form template in Design mode.
  3. Double-click the Button control to open its Properties dialog box.
  4. On the Button Properties dialog box, click Edit Form Code.
  5. Add the following code to the Clicked event handler of the Button control:

    C#
    XPathNavigator domNav = MainDataSource.CreateNavigator();
    XPathNodeIterator rows = domNav.Select(
    "/my:myFields/my:table/my:row", NamespaceManager);
    
    while (rows.MoveNext())
    {
    string field1 = rows.Current.SelectSingleNode(
    "my:field1", NamespaceManager).Value;
    string field2 = rows.Current.SelectSingleNode(
    "my:field2", NamespaceManager).Value;
    string field3 = rows.Current.SelectSingleNode(
    "my:field3", NamespaceManager).Value;
    } 



    Visual Basic
    Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()
    Dim rows As XPathNodeIterator = domNav.Select( _
    "/my:myFields/my:table/my:row", NamespaceManager)
    
    While rows.MoveNext()
    Dim field1 As String = rows.Current.SelectSingleNode( _
    "my:field1", NamespaceManager).Value
    Dim field2 As String = rows.Current.SelectSingleNode( _
    "my:field2", NamespaceManager).Value
    Dim field3 As String = rows.Current.SelectSingleNode( _
    "my:field3", NamespaceManager).Value
    End While 
    

    Save your work, build the project, and test the form template.

delete the first row of a repeating table in InfoPath

http://www.bizsupportonline.net/infopath2007/programmatically-delete-first-row-repeating-table-infopath.htm

You can achieve this functionality as follows:

  1. Create a new blank InfoPath form template, and add a Repeating Table and aButton control to it.


    Figure 1. The the form template in Design mode.

    The Main data source is shown in the following figure:


    Figure 2. The Main data source of the form template.

  2. Double-click the Button control to open its Properties dialog box.
  3. On the Button Properties dialog box, click Edit Form Code to add aClicked event handler for the button.
  4. Add the following C# code to the Clicked event:

    // Retrieve the first row of the repeating table
    XPathNavigator domNav = MainDataSource.CreateNavigator();
    XPathNavigator itemNav = domNav.SelectSingleNode(
    "/my:myFields/my:group1/my:group2[1]",
    NamespaceManager);
    
    // Delete the row
    if (itemNav != null)
    itemNav.DeleteSelf();
    


    Or add the following Visual Basic code to the Clicked event:

    ' Retrieve the first row of the repeating table
    Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()
    Dim itemNav As XPathNavigator = domNav.SelectSingleNode( _
    "/my:myFields/my:group1/my:group2[1]", _
    NamespaceManager)
    
    ' Delete the row
    If itemNav IsNot Nothing Then
    itemNav.DeleteSelf()
    End If

     

    Note: You can use the Copy XPath functionality in the Data Source pane (also see Programmatically retrieve the value of an InfoPath form field using .NET code) to find out what the XPath expression of thegroup2 node should be. Then you can add the XPath expression filter[1] to retrieve the first row of the repeating table.

  5. Save your work, build the code, and test the form.

 

4 Ways to programmatically add a row to a repeating table in InfoPath


Method 1 - Use a string to add a row to a repeating table in Infopath

In the following sample code the XML for the row is constructed using a StringBuilder object and then passed to the AppendChild method as a string to add a row to a repeating table in InfoPath.

string my = NamespaceManager.LookupNamespace("my");
StringBuilder sb = new StringBuilder();
sb.Append("<my:group2 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("<my:field1 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("Cell 1");
sb.Append("</my:field1>");
sb.Append("<my:field2 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("Cell 2");
sb.Append("</my:field2>");
sb.Append("<my:field3 xmlns:my=\"");
sb.Append(my);
sb.Append("\">");
sb.Append("Cell 3");
sb.Append("</my:field3>");
sb.Append("</my:group2>");
MainDataSource.CreateNavigator().SelectSingleNode(
&nsbp; "/my:myFields/my:group1", NamespaceManager).AppendChild(sb.ToString());


Method 2 - Use an XPathNavigator object to add a row to a repeating table in InfoPath

In the following sample code an XmlDocument is used to construct the XML for a row and then an XPathNavigator object is created from the document element of thisXmlDocument and passed to theAppendChild method to add a row to a repeating table in InfoPath.

XmlDocument doc = new XmlDocument();
XmlNode group = doc.CreateElement("group2", NamespaceManager.LookupNamespace("my"));

XmlNode field = doc.CreateElement("field1", NamespaceManager.LookupNamespace("my"));
XmlNode node = group.AppendChild(field);
node.InnerText = "Cell 1";

field = doc.CreateElement("field2", NamespaceManager.LookupNamespace("my"));
node = group.AppendChild(field);
node.InnerText = "Cell 2";

field = doc.CreateElement("field3", NamespaceManager.LookupNamespace("my"));
node = group.AppendChild(field);
node.InnerText = "Cell 3";

doc.AppendChild(group);

MainDataSource.CreateNavigator().SelectSingleNode(
  "/my:myFields/my:group1",
  NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

在此,group中赋值的顺序需与 数据源中的顺序一致,否则会出错.

Method 3 - Use an XMLReader object to add a row to a repeating table in InfoPath

In the following sample code a FileStream object is used to read an XML file that contains the XML structure for a row. It then creates anXmlReader object from theFileStream and passes it to theAppendChild method to add a row to a repeating table in InfoPath.

Contents of a file named row.xml that is located on the C-drive:

<my:group2 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">
  <my:field1 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">Cell 1</my:field1>
  <my:field2 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">Cell 2</my:field2>
  <my:field3 xmlns:my="the_xml_namespace_of_your_form_template_goes_here">Cell 3</my:field3>
</my:group2>


Code to add a row to the repeating table in InfoPath:

using (FileStream fs = new FileStream(@"C:\row.xml", FileMode.Open))
{
  using (XmlReader reader = XmlReader.Create(fs))
  {
    MainDataSource.CreateNavigator().SelectSingleNode(
      "/my:myFields/my:group1", NamespaceManager).AppendChild(reader);
    reader.Close();
  }
  fs.Close();
}


Method 4 - Use an XmlWriter object to add a row to a repeating table in Infopath

See Programmatically add a row to a repeating table using an XmlWriter object

 

 

Programmatically execute code when a repeating table row is inserted or deleted

 

  • Follow the instructions in Programmatically execute code when a repeating table row is inserted or deleted, but use a Repeating Section control instead of a Repeating Table control.
  • Add an extra Text Box control to the form template, name it runningCount, and set its Default Value to 1.
  • Add the following C# code to the Changed event handler of the group2 node:

    XPathNavigator root = MainDataSource.CreateNavigator();
    
    // Count the amount of sections whenever a section is inserted or deleted
    if (e.Operation == XmlOperation.Insert || e.Operation == XmlOperation.Delete)
    {
    XPathNodeIterator iter = root.Select("//my:group2", NamespaceManager);
    
    if (iter != null)
    root.SelectSingleNode(
    "//my:runningCount", 
    NamespaceManager).SetValue(iter.Count.ToString());
    }


    Or add the following Visual Basic code to the Changed event handler of the group2 node:

    Dim root As XPathNavigator = MainDataSource.CreateNavigator()
    
    ' Count the amount of sections whenever a section is inserted or deleted
    If e.Operation = XmlOperation.Insert Or e.Operation = XmlOperation.Delete Then
    
    Dim iter As XPathNodeIterator = root.Select("//my:group2", NamespaceManager)
    
    If iter IsNot Nothing Then
    root.SelectSingleNode( _
    "//my:runningCount", _
    NamespaceManager).SetValue(iter.Count.ToString())
    End If
    
    End If
    



     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值