There are few ways we can sort the data in InfoPath repeating table. However, there is no out of the box sorting mechanism in InfoPath. (I think it is doable but very tedious). Here is very simple code, we can sort the data with multiple fields (columns).
Generally, explicit sorting is not required if you bind data from Database, as we can include our sorting login in SQL query. What, if you are getting data from different sources. Here is the code.
I am assuming that, on button click we will sort the data in Repeating table.
Cheers!
Vamsi
Generally, explicit sorting is not required if you bind data from Database, as we can include our sorting login in SQL query. What, if you are getting data from different sources. Here is the code.
I am assuming that, on button click we will sort the data in Repeating table.
public void btnSortData_Clicked(object sender, ClickedEventArgs e)
{
// Replace with your respective xpath and
field names
String xpathRow = "/my:myFields/my:ManageProjectViewWrapper/my:SubProjectsSum/my:SUBPROJECTS";
// you can add as many fields as
you can
String[] xpathField = { "my:subprojecTitle",
“my:subprojecID” };
XmlSortOrder[] order = { XmlSortOrder.Ascending,
XmlSortOrder.Ascending };
XmlDataType[] type = { XmlDataType.Text, XmlDataType.Text };
SortData(xpathRow, xpathField, order, type);
}
public bool SortData(String row, String[] field, XmlSortOrder[] order, XmlDataType[] type)
{
bool returnVal = true;
try
{//
you should have more than 2 rows for sorting
XPathNavigator myNamespace =
MainDataSource.CreateNavigator();
XPathExpression myRow = myNamespace.Compile(row);
myRow.SetContext(NamespaceManager);
XPathNodeIterator myRowIterator = myNamespace.Select(myRow);
if (myRowIterator.Count > 2)
{// Loop through parameters to add sorting.
for (int i = 0; i < field.Length;
i++)
{// Set the various sort parameters.
XmlSortOrder myOrder
= order[i];
XmlDataType myDataType =
type[i];
XPathExpression myField
= myNamespace.Compile(field[i]);
myField.SetContext(NamespaceManager);
// Add sort parameter to compiled row XPath.
myRow.AddSort(myField,
myOrder, XmlCaseOrder.None, "",myDataType);
}
}
//
Select table and sorted node set.
XPathNavigator myTable = myNamespace.SelectSingleNode(row,
NamespaceManager);
myTable.MoveToParent();
myRowIterator =
myNamespace.Select(myRow);
//
Delete unsorted rows.
XPathNavigator xnRangeFirst = myNamespace.SelectSingleNode(row +
"[1]", NamespaceManager);
XPathNavigator xnRangeLast = myNamespace.SelectSingleNode(row +
"[last()]", NamespaceManager);
xnRangeFirst.DeleteRange(xnRangeLast);
//
Append sorted rows.
while (myRowIterator.MoveNext())
{myTable.AppendChild(myRowIterator.Current);
}
}//
end of try
catch
{
returnVal = false;
}
return returnVal;
}
Happy programming....Cheers!
Vamsi
No comments:
Post a Comment