Problem
You have a drop-down list box in InfoPath, which you would like to dynamically populate (=fill) through code with items from anXML document.
Solution
Bind the drop-down list box to a repeating node in either the Main or a secondary data sourceof the InfoPath form, and then loop through the nodes in the XML document to populate the data source that is bound to
the drop-down list box with the data from the XML document.
Discussion
A drop-down list box in InfoPath consists of:- A field in which to store the value of the item that is selected from the drop-down list box. This is the field that you drag-and-drop onto the InfoPath form and
which represents the drop-down list box. - A repeating node in the Main or a secondary data source, which the drop-down list box is bound to, and
which contains the items to display in the drop-down list box.
instead of setting the value of the drop-down list box. The latter will set the selected value on the drop-down list box
instead of filling the drop-down list box with items.
This is an important concept to understand when using list boxes in InfoPath and wanting to fill them programmatically
with items.
You can accomplish this functionality as follows:
- Open Microsoft Office InfoPath 2007, create a new blank form, and add a Drop-Down List Box
to the form (also see: Insert a drop-down list box). Your form should resemble the following figure:
Figure 1. The InfoPath form template in Design mode.
Create an XML file with the following content:
<?xml version="1.0" encoding="UTF-8" ?>
<lists>
<list><title/></list>
<list><title/>
</list>
</lists>
This XML file will serve as a secondary data source that will contain the items for the drop-down list box, so create
a Receive data connection to an XML document for this XML file, and bind
the Drop-Down List Box to the data connection you created
(also see: Working with the data source and data connections).
Note: The XML file contains two list nodes instead of one. This is necessary for
InfoPath to recognize the list node as a repeating node. One of these
nodes will be deleted later through code before populating the drop-down list box. The remaining node will
be copied repeatedly to create the items for the drop-down list box.
Figure 2. Binding the drop-down list box to a secondary data source.
- Create an XML document and add a Receive data connection for this XML document
to the InfoPath form template. You will use this secondary data source to populate the secondary data source that
is bound to the drop-down list box, so that the content of this XML document will show up as items in the drop-down
list box. Note: You are not required to use an XML document to populate the drop-down list box, but could use any
other type of data source or file that contains rows of data, which can be loaded into InfoPath, looped through,
and used to populate
the secondary data source that is bound to the drop-down list box.
Add the following code to the Loading event handler of the form:
C#
RemoveFirstItem();
XPathNodeIterator players = DataSources["doclibs"].CreateNavigator().Select(
"//player", NamespaceManager);
foreach (XPathNavigator player in players)
{
string title = player.SelectSingleNode("title", NamespaceManager).Value;
AddItem(title);
}
Add the following code for the RemoveFirstItem and AddItem methods to the form's code.
The RemoveFirstItem method removes one of the list nodes from the secondary data source that is bound to
the drop-down list box (see note in step 2). The AddItem method adds an item to the secondary data source
that is bound to the drop-down list box, thereby filling the title.
for each item that will appear in the drop-down list box.
C#
private void AddItem(string title)
{
XPathNavigator nav = this.DataSources["doclibs"].CreateNavigator();
XPathNavigator group1 = nav.SelectSingleNode("/lists", NamespaceManager);
XPathNavigator node = group1.SelectSingleNode("list", this.NamespaceManager);
XPathNavigator newNode = node.Clone();
newNode.SelectSingleNode("title").SetValue(title);
group1.AppendChild(newNode);
}
private void RemoveFirstItem()
{
XPathNavigator firstNode = this.DataSources["doclibs"].CreateNavigator().SelectSingleNode("/lists/list", this.NamespaceManager);
firstNode.DeleteSelf();
}
- Save your work, build the project, and test the form.
You should now have a fully functional InfoPath form so that once the form loads, the items from the XML document
will appear as items in the drop-down list box.
0 comments:
Post a Comment