where SoNFields/SoNField is populating from Secondary Data Source "AGFileEntities" which is sorted according to SortOrder
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
/* ** For Sorting purpose ** */
if (!FormState.Contains("InSwap"))
FormState.Add("InSwap", false);
/* ** For Sorting purpose ** */
XPathNavigator root = this.MainDataSource.CreateNavigator();
XPathNodeIterator nodes;
XPathNavigator clone;
XPathNavigator pListRoot = this.DataSources["AGFileEntities"].CreateNavigator();
SortList(pListRoot, "SortOrder", "/dfs:myFields/dfs:dataFields/dfs:IPS_Auto_Generated_File_Entities");
nodes = pListRoot.Select("/dfs:myFields/dfs:dataFields/dfs:IPS_Auto_Generated_File_Entities[@Field_Type='Preliminary SoN']", this.NamespaceManager);
clone = root.SelectSingleNode("/my:myFields/my:preliminarySoN/my:SoNFields/my:SoNField", this.NamespaceManager).Clone();
while (nodes.MoveNext())
{
clone.SelectSingleNode("my:Title", this.NamespaceManager).SetValue(nodes.Current.GetAttribute("Title", "").Trim());
clone.SelectSingleNode("my:Description", this.NamespaceManager).SetValue(nodes.Current.GetAttribute("Description", "").Trim());
root.SelectSingleNode("/my:myFields/my:preliminarySoN/my:SoNFields", this.NamespaceManager).AppendChild(clone);
}
root.SelectSingleNode("/my:myFields/my:preliminarySoN/my:SoNFields/my:SoNField", this.NamespaceManager).DeleteSelf();
}
else
{
}
}
#region Sorting
private void SortList(XPathNavigator aListRoot, string sortBy, string itemsToSort)
{
System.Globalization.CultureInfo currentThreadCulture =
System.Threading.Thread.CurrentThread.CurrentCulture;
int numPeople = aListRoot.Select(itemsToSort, NamespaceManager).Count;
/* basic bubble sort implementation */
for (int i = 1; i < numPeople; i++) /* xpath is 1-based */
{
for (int j = i + 1; j <= numPeople; j++) // keep j ahead of i; we can index [numPeople]
{
/* swap (i,j) if i > j */
string iValue = GetValue(aListRoot, itemsToSort + WrapAsIndexer(i) + "/@" + sortBy);//GetValue(itemsToSort + WrapAsIndexer(i) + "/dfs:" + sortBy);
string jValue = GetValue(aListRoot, itemsToSort + WrapAsIndexer(j) + "/@" + sortBy);//GetValue(itemsToSort + WrapAsIndexer(j) + "/dfs:" + sortBy);
/* Do we sort by number or string? */
/*if (SortAsNumber) */ /*{ */ int iNum, jNum;
if (!
Int32.TryParse(iValue,
out iNum) || !
Int32.TryParse(jValue,
out jNum))
{
/* Let InfoPath take care of the invalid datatype with its own validation, we'll keep sorting the rest */ continue;
}
if (iNum > jNum)
{
Swap(aListRoot, itemsToSort + WrapAsIndexer(i), itemsToSort + WrapAsIndexer(j));
}
/*}*/
/*else // SortAsString */
/*{ */
/* if (String.Compare( */
/* iValue, jValue, true */ /*ignoreCase*//*currentThreadCulture) > 0) */
/* { */
/* Swap(itemsToSort + WrapAsIndexer(i), itemsToSort + WrapAsIndexer(j)); */
/* } */
/*} */
} /* end inner-for */
} /* end outer-for */
}
private string WrapAsIndexer(int intToWrap)
{ return WrapAsIndexer(intToWrap.ToString()); }
private string WrapAsIndexer(string strToWrap)
{ return "[" + strToWrap + "]"; }
private string GetValue(XPathNavigator aListRoot, string xpath)
{ return aListRoot.SelectSingleNode(xpath, NamespaceManager).Value; }
private void SetValue(XPathNavigator aListRoot, string xpath, string value)
{ aListRoot.SelectSingleNode(xpath, NamespaceManager).SetValue(value); }
private void Swap(XPathNavigator aListRoot, string xpath1, string xpath2)
{
InSwap = true;
try
{
XPathNavigator item1 = aListRoot.SelectSingleNode(xpath1, NamespaceManager);
XPathNavigator item2 = aListRoot.SelectSingleNode(xpath2, NamespaceManager);
XPathNavigator item1Clone = item1.Clone();
item1.ReplaceSelf(item2);
item2.ReplaceSelf(item1Clone);
}
finally /* always undo the reentrancy block at the end */
{
InSwap = false;
}
}
private bool InSwap
{
get { return (bool)FormState["InSwap"]; }
set { FormState["InSwap"] = value; }
}
#endregion