I am using the Attribute Table from the Community Examples: arcgis-pro-sdk-community-samples .
I am loading a selected FeatureLayer and it display's in the table fine.
I want to sort the table by a Field called "SortField".
I looked at the Answer from this question: Sort Table based on a given field , and decided to try it.
Firstly, the custom Attribute Table does not extend TableView, it extends UserControl, but I found a Way to get the TableControl, which has the same properties, methods and even handy Events.
So using this answer:
var tv = TableView.Active;
if (tv == null)
return;
// sort the active field descending
if (tv.CanSortDescending)
tv.SortDescending();
// sort the active field ascending
if (tv.CanSortAscending)
tv.SortAscending();
// peform a custom sort programmatically
if (tv.CanCustomSort)
{
// sort fields
var dict = new Dictionary<string, FieldSortInfo>();
dict.Add("STATE_NAME", FieldSortInfo.Asc);
dict.Add("CITY_NAME", FieldSortInfo.Desc);
await tv.SortAsync(dict);
}
// perform a custom sort via the UI
if (tv.CanCustomSort)
tv.CustomSort();
I wrote this:
private void CatalogueTableContentLoaded(object? sender, EventArgs e)
{
//Inside an Event handler that is invoked when the FeatureLayer has been
//loaded into the table's grid.
QueuedTask.Run(async () =>
{
TableControl tc = null;
await FrameworkApplication.Current.Dispatcher.Invoke(async () =>
{
tc = sender as TableControl;
//The event is invoked before the grid draw is complete, so if we
//wait a shorter time than this, tc.CanCustomSort will be false.
await Task.Delay(1500);
});
if (tc.CanCustomSort)
{
//I tried this....
var dict = new Dictionary<string, FieldSortInfo>();
dict.Add($"{SortField}", FieldSortInfo.Desc);
await tc.SortAsync(dict);
//And I tried this...
SelectedFeatureLayerWrapper.SortDecending([SortField]);
}
}).ConfigureAwait(true).GetAwaiter();
}
//Here is FeatureLayerWrapper.SortDecending
public void SortDecending(string[] fieldList)
{
QueuedTask.Run(() =>
{
var cimLayer = GetDefinition() as CIMFeatureLayer;
var sortInfoList = new List<CIMFeatureSortInfo>();
// Create a new sort info object
foreach (var fieldName in fieldList)
{
var sortInfo = new CIMFeatureSortInfo();
sortInfo.FieldName = fieldName; // Name of the field to sort by
sortInfo.SortDirection = SortOrderType.Descending;
sortInfoList.Add(sortInfo);
}
// Assign to FeatureSortInfos (accepts an array)
cimLayer.FeatureSortInfos = sortInfoList.ToArray();
// Apply the definition back to the layer
SetDefinition(cimLayer);
}).ConfigureAwait(false).GetAwaiter();
}
When this code is completed, using either the TableSort or the FeatureLayer sort, the table remains unsorted.
Goggle AI suspects the TableControl in the custom Attribute Table, is not truely linked to the grid, but I can get the Events just fine.
Using the FeatureLayer sort, the table flashes, as though responding to the sort, but the sort doesn't take. I think this is because the sort isn't permanent using the FeatureLayer sort.
I even tried sorting the FeatureClass and using a Select , but the table freezes then.
Any insight or better ideas would be appreciated.
Thanks in advance.