Build Repeating Table in Browser Enabled Form

F

FrankS

VS2005, InfoPath 2007, SQL2005, managed VB

Have exisitng projects that build Repeating Table using
CurrentView.ExecuteAction(ActionType.XCollectionInsert,
"RepeatingTableName"). Can you give me a workable example of 1) inserting
rows and 2) using xpathnav to populate row in Browser enabled form. VB or C#
is fine. Thanks.

Best regards,
Frank
 
D

David Dean

To insert rows in a browser-enabled form, you have to generate the XML for
the repeating table row, inserting it into the main data source at the
appropriate location. The row will then "magically" appear.

Here's a code example:

XPathNavigator main = this.MainDataSource.CreateNavigator();
XPathNavigator sectionNode = main.SelectSingleNode("/my:myFields/my:group1",
this.NamespaceManager);
string ns = sectionNode.NamespaceURI;
using (XmlWriter xw = sectionNode.AppendChild())
{
xw.WriteStartElement("my", "group2", ns);
xw.WriteElementString("my", "field1", ns, "Value for field 1");
xw.WriteElementString("my", "field2", ns, "Value for field 2");
xw.WriteElementString("my", "field3", ns, "Value for field 3");
xw.WriteEndElement();
}
 
R

rsilva

Here is another way to accomplish this. This is based on a timesheet
form with dynamically created date ranges for input:

The schema uses 2 repeating tables, payroll periods have 4 or 5 week(s)
and each has 7 day(s):

<weeks>
<week>
<days>
<day><other_fields/></day>
</days>
</week>
</weeks>


The following C# managed code builds the date range array, then loops
each day and determines its week and day nodes and creates them if they
don't exist. You can see that I grab an existing node and just
AppendChild to its parent node.

private void BuildTimesheet(DateTime startingDate, DateTime endingDate)
{
// Calculate the date range
DateTime[] dtRange = this.FillDays(startingDate, endingDate);

XPathNavigator root = this.MainDataSource.CreateNavigator();
// Set some initial node elements
XPathNavigator nodeWeeks =
root.SelectSingleNode("/my:TimesheetData/my:weeks", NamespaceManager);
XPathNavigator nodeWeek =
nodeWeeks.SelectSingleNode("my:week", NamespaceManager);
XPathNavigator nodeDay;

foreach (DateTime date in dtRange)
{
string dayOfWeek = ((int)date.DayOfWeek + 1).ToString();
nodeDay = nodeWeek.SelectSingleNode("my:days/my:day[" +
dayOfWeek + "]", NamespaceManager);
if (nodeDay == null)
{
XPathNavigator tmpNodeDays =
nodeWeek.SelectSingleNode("my:days", NamespaceManager);
XPathNavigator tmpNodeDay =
tmpNodeDays.SelectSingleNode("my:day[1]", NamespaceManager);
tmpNodeDays.AppendChild(tmpNodeDay);
nodeDay =
nodeWeek.SelectSingleNode("my:days/my:day[" + dayOfWeek + "]",
NamespaceManager);
}
// Fill in the Date using "Jan 13" format
XPathNavigator myDate =
nodeDay.SelectSingleNode("my:date", NamespaceManager);
this.SetNilValue(myDate, String.Format("{0} {1}",
date.ToString("MMM"), date.ToString("%d")));

// Create a new week node and append it
if (date.DayOfWeek == DayOfWeek.Saturday && date !=
endingDate)
{
XPathNavigator tmpWeekNode =
nodeWeeks.SelectSingleNode("my:week[1]", NamespaceManager);
nodeWeeks.AppendChild(tmpWeekNode);
nodeWeek =
nodeWeeks.SelectSingleNode("my:week[last()]", NamespaceManager);
}
}

}

Bob Silva
 
F

FrankS

Hey Dave. Thanks for the quick response. I could not get this to work. I came
up with a workable solution. Using your schema this works:
-------------------
Dim main As XPathNavigator = MainDataSource.CreateNavigator()
Dim root As XPathNavigator =
main.SelectSingleNode("/my:myFields", Me.NamespaceManager)
Dim sectionNode As XPathNavigator =
MainDataSource.CreateNavigator.SelectSingleNode("/my:myFields/my:Group1",
Me.NamespaceManager)

root.AppendChild(sectionNode)
root.SelectSingleNode("/my:myFields/my:Group1/my:field1",
NamespaceManager).SetValue("Value for field 1")
root.SelectSingleNode("/my:myFields/my:Group1/my:field2",
NamespaceManager).SetValue("Value for field 2")
root.SelectSingleNode("/my:myFields/my:Group1/my:field3",
NamespaceManager).SetValue("Value for field 3")

End Sub
-----------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top