Select to view content in your preferred language

Display QueryTable in WPF DataGrid using ArcGIS Pro SDK

705
3
09-11-2023 02:24 AM
Eleonorka
New Contributor

Hello Everybody,

I am working on add-in for ArcGIS Pro 3.1, the code is in C#.

The tool acesses data in Oracle database, create a QueryTable that I want to display in a WPF DataGrid Control.

I use snippet from ProSnippets Geodatabase to get QueryTable, which works, but I don't know how to open the table in the WPF DataGrid.

I get an error: 

System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

I use QueuedTask.Run to open the QueryTable, but it is probably in a different Thread. How to access QueryTable from WPF?

 

Here is my code:

public partial class Window1 : Window
{

   private async void clsBtn_Click(object sender, RoutedEventArgs e)
   {
   //open Query Table
   var t = await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
   {

   using (Geodatabase sdedb01 = new Geodatabase(Module1.connectionProperties2))
   {
      QueryDef queryDef = Module1.GetQueryDef();

      QueryTableDescription queryTableDescription = new QueryTableDescription(queryDef)
      {
      Name = "wellsView",
      PrimaryKeys = sdedb01.GetSQLSyntax().QualifyColumnName("DAT_GDO_GF.GEO_PROFIL",         "KLIC_GDO") + ", " +
   sdedb01.GetSQLSyntax().QualifyColumnName("DAT_GDO_GF.GEO_PROFIL", "KLIC_GEO") + ", " +
   sdedb01.GetSQLSyntax().QualifyColumnName("DAT_GDO_GF.GEO_HOR", "POR_HOR")
      };

      ArcGIS.Core.Data.Table tbl = sdedb01.OpenQueryTable(queryTableDescription);

      //here the QueryTable works:

      MessageBox.Show("Number of rows in querytable je: " + Convert.ToString(tbl.GetCount()));
      return tbl;
   }
});

   //ERROR, System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it

   dataGrid1.DataContext = t;

}

 

Thanks for any help!

Lenka

0 Kudos
3 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

There is no direct way to specify ArcGIS Pro Table to WPF datagrid.

I would recommend you use ArcGIS Pro TableControl instead of WPF datagrid.

1. Add your result 'tbl' to map and get StandaloneTable.

 

 

    // Inside QueuedTask.Run
    var table_params = new StandaloneTableCreationParams(tbl);
    var standaloneTable = StandaloneTableFactory.Instance.CreateStandaloneTable(table_params, MapView.Active.Map);‍‍
    return standaloneTable

 

 

2. Create TableControlContent from StandaloneTable.

 

 

        // create the content
        var tableContent = TableControlContentFactory.Create(standaloneTable);

 

 

3. Specify TableControl TableContent property with your created TableControlContent

 

 

    tableControl.TableContent = tableContent;

 

 

 

 Sample with TableControl here.

https://github.com/Esri/arcgis-pro-sdk/wiki/ProGuide-TableControl 

Eleonorka
New Contributor

Thank you very much! TableControl could be what I should use for displaying QueryTables.

But is there any other way to create TableControl Content than from Standalone Table?

I need the TableControl to dispay QueryTable everytime the user click on the object in the map or enter the object ID in the WPF form. Creating new standalone tables takes time and creates a lot of items in the TOC.

Thanks for your help!

Elenka

0 Kudos
GKmieliauskas
Esri Regular Contributor

TableControlContentFactory Create method has 2 overloads: from MapMember and Item. I use MapMember for creating TableControlContent. You can try to use Item. You need table path to create Item.

// inside QueuedTask.Run
// Not sure is it correct path. Try
string tblPath = Path.Combine(sdedb01.GetPath().AbsolutePath, "wellsView");
return tblPath;

// outside QueuedTask.Run
var item = ItemFactory.Instance.Create(t);
var tableContent = TableControlContentFactory.Create(item);
0 Kudos