Add items to dropdown in a Infopath 2007 Web Form
Hi
I would like to add extra items to a dropdown box from codebehind in a infopath document (c#). I have trid serveral solutions but i can not get it to work. I always get a Schema validation error. I have tried something like this.
// FILL DROPDOWN
XPathNavigator myRoot = MainDataSource.CreateNavigator();
XPathNavigator the_ddl_node = null;
the_ddl = myRoot.SelectSingleNode("/my:mineFelter/my
ection_s/my:the_ddl", NamespaceManager);
the_ddl.InsertAfter("<my:the_ddl>A new item</the_ddl>");
But it does not work
anderskj1 wrote: |
| Hi I would like to add extra items to a dropdown box from codebehind in a infopath document (c#). I have trid serveral solutions but i can not get it to work. I always get a Schema validation error. I have tried something like this. // FILL DROPDOWN XPathNavigator myRoot = MainDataSource.CreateNavigator(); XPathNavigator the_ddl_node = null;the_ddl = myRoot.SelectSingleNode("/my:mineFelter/myection_s/my:the_ddl", NamespaceManager); the_ddl.InsertAfter("<my:the_ddl>A new item</the_ddl>"); But it does not work | |
Yeah, I've got nearly the same question. How can I manage drop-down list porgrammatically?
F...k!!!!!!!!!!!!!!!!!
...I've spend a week trying to upload an ArrayList into the Drop-down InfoPath control and did nothing!!!!
1 attempt :
XPathNavigator Item = root.SelectSingleNode("/my:myFields/my:table/my:group2/my:name", _senderObject.NamespaceManager);
//XmlSchemaSet schemaSet = new XmlSchemaSet(Item.NameTable);
//schemaSet.ValidationEventHandler +=new ValidationEventHandler(schemaSet_ValidationEventHandler);
//if (Item.CheckValidity(schemaSet, schemaSet_ValidationEventHandler))
//Here I tried to validate an Xml Schema by this code and received an exception message : Not Implemented!
//1.
Item.AppendChildElement(Item.Prefix, "name", Item.NamespaceURI, "Text");
//2.
Item.AppendChild("<my:name xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-10-12T06:46:39\">Text</my:name>");
//3.
XmlWriter writer = Item.AppendChild();
// Write XML data.
writer.WriteStartElement("my", "name", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-10-12T06:46:39");
writer.WriteString("Text");
writer.WriteEndElement();
writer.Close();
//Here I have :
"Schema validation found non-data type errors" exception.
2 attempt :
XPathNodeIterator nodeItems = nameNode.CreateNavigator().Select("/my:myFields/my:table/my:group2/my:name", _senderObject.NamespaceManager);
for (int i = nodeItems.Count - 1; i >= 0; i--)
{
XPathNavigator currentNode = nodeItems.Current;
XPathNavigator Item = currentNode.CreateNavigator().SelectSingleNode("/my:myFields/my:table/my:group2/my:name[last()]", _senderObject.NamespaceManager); //I supose this is redundant, but I would like to be pointed out on my mistakes.
XPathNodeIterator itemChildren = Item.SelectChildren("name", Item.NamespaceURI);
XPathNavigator child = itemChildren.Current;
child.AppendChild("<my:name xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-10-12T06:46:39\">Text</my:name>");
//Here I have :
"Schema validation found non-data type errors" exception.
}
So :
"Schema validation found non-data type errors" exception.
How can I add, delete or update my InfoPath Drop-Down List content?
Thank you!!
It looks like you aren't trying to add items to element, not a group. If you change XPath expression you have to something like ".../my:group" and use AppendChild it should start working.
I have a repeating table with a columns in it. Some columns have drop-down lists connected to SQL data source. How can I programmatically change, edit, clear, add...e.t.c. elements from this lists?
XPath of my column is /my:myFields/my:table/my:group2/my:name
So I tried as you suggested :
XPathNavigator nameNode = root.SelectSingleNode("/my:myFields/my:table/my:group2", _senderObject.NamespaceManager);
nameNode.AppendChild("<my:name xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-10-12T06:46:39\"></my:name>");
This also doesn't work.
I suppose it is incorrect as I add child node, but I would like to add additional element in a child node called "name".
Besides how can I see XML schema of my form?
Thank you.
Open "data source" task pane while designing form to see schema. You may need to choose secondary data source where items for drop-downs are coming from.
Likely drop-down lists are using data from Secondary data source. To change list of items for drop-down you need to change XML for secondary data source.
You should be adding whole repeating group/row to proper parent (in your case XML to insert likely will be <my:group2><my:name/><my:group2/> and parent is probably my:table).
If you want to just change value of the node you can use setvalue/replaceself methods of XPathNavigator after selecting my:name node.
Yes!!!
Just discovered that way of solving my problem. And read the confirmation of my solution in your post!
Спасибо, коллега!!!!! ;-))))
For adding data to the drop down list in Infopath 2007, do the following method.
First add a field, check "Repeating" checkbox and select this field in the drop down control from "Look up values in the form's data source".
Then using the following code, add the data.
Here "ddLocation" is the drop down and "Location" is the repeating field selected in the drop down.
Conn.Open();
xPathNav.SelectSingleNode(
"/my:myFields/my
dLocation", NamespaceManager).SetValue(""); SqlCommand cmd = new SqlCommand("slp_SelectLeaveLocation", Conn); SqlDataAdapter sqlData = new SqlDataAdapter(cmd); DataSet dsLoc = new DataSet(); sqlData.Fill(dsLoc);
XPathNavigator Loc = xPathNav.SelectSingleNode("/my:myFields/my:LeaveTypeData/my:Location", this.NamespaceManager); Loc.SetValue(
""); XPathNavigator templateNode = Loc.Clone(); foreach (DataRow dr in dsLoc.Tables[0].Rows) {
XPathNavigator newNode = templateNode.Clone(); string value = dr[0].ToString(); newNode.SetValue(value);
Loc.InsertAfter(newNode);
}
Loc.DeleteSelf();
Conn.Close();
2 sasireka
Do you like beer? ;-))) If you were a little bit closer, I'd treat to you a couple of pints.
THANK YOU VERY MUCH!!!!! IT REALLY WORKS!!!!
Something stupied -> how can i clear the list before refill it ?
I d like to thank you for your help, was a problem that kills my faith this week and i find it in your quote.
Hi,
This might help you delete the list except the first node.
XPathNodeIterator Loc_del = myNavigator1.Select("/my:XML_Select_group/my:Repeating_Field", NamespaceManager);
if (Loc_del.Count > 1)
{
string myGroup = "/my:XML_Select_group/my:Repeating_Field";
XPathNavigator first = myNavigator1.SelectSingleNode(myGroup + "[2]", NamespaceManager);
XPathNavigator last = myNavigator1.SelectSingleNode(myGroup +"[position()=last()]", NamespaceManager);
first.DeleteRange(last);
}
If you want to delete first node as well you can do by replacing this code...
XPathNavigator first = myNavigator1.SelectSingleNode(myGroup + "[1]", NamespaceManager);
Thank You. Sorry I'm late to answer but I was for 1.5 month out of office.
This is great stuff. But what if you drop downs are in a repeating section?
I have three drop downs, a Category, a Type and a SubType. The Type drop down is dependent on the selected Category and the SubType is dependent on the selected Type. I have an event on the Category changed that will load the Type drop down, but when there are more than one row in the repeating section, the code changes "all" the Type dropdowns values, not just the active one.
Can somebody please put the code here which work perfectly by just giving Dataset Or XMLNode and Drop-Down List Name ?
I'm new to InfoPath and don't know what to do ? As I've read in the post that code given by sasireka is working fine for you ppl but its not working for me.
Regards,
Khurram
Its giving me error that xPathNav is not declared. How to declare xPathNav and what to assign it ?