Arcobjects change table data source

5646
3
Jump to solution
03-13-2015 04:42 AM
JonMorris2
Occasional Contributor II

I'm writing a C# addin to change all the data sources in an MXD from one SDE datasource to another. I can iterate through the layers and change feature layers and raster layers, but what about tables?

There is very little doc on this - it can be done in arcpy, but I could only find one old unanswered question for how to do it in arcobjects.

I've used the TableCollection to iterate through the tables in the map, but it's not clear how to update the source for each one. Any ideas?

Reply
0 Kudos
1 Solution

Accepted Solutions
JonMorris2
Occasional Contributor II

Anyone? Never mind, I've figured it out:

  // focus map

  IMap map = ...

  // new data source for table

  IFeatureWorkspace workspace = ...

  ITableCollection tableCollection = (ITableCollection)map;

  List<ITable> tables = new List<ITable>();

  for (int i = 0; i < tableCount; i++)

  {

  tables.Add(tableCollection.get_Table(i));

  }

  // iterate over a copy of the tables so we can add and remove

  foreach (ITable oldTable in tables)

  {

  IDataset dataset = (IDataset)oldTable;

  IDatasetName datasetName = (IDatasetName)dataset.FullName;

  // assumes table name is same in new data source

  // get the new table

  ITable newTable = workspace.OpenTable(datasetName);

  // replace the table in the TOC

  ITableCollection tableCollection = (ITableCollection)map;

  tableCollection.RemoveTable(oldTable);

  tableCollection.AddTable(newTable);

  // fire change event to change table

  IMapAdmin2 mapAdmin2 = (IMapAdmin2)map;

  mapAdmin2.FireChangeTable(oldTable, newTable);

  }

  // Redraw the map

  IActiveView activeView = (IActiveView)map;

  activeView.Refresh();

  // Refresh TOC

  ESRI.ArcGIS.ArcMapUI.IContentsView contentsView = ArcMap.Document.CurrentContentsView;

  contentsView.Refresh(null);

View solution in original post

Reply
0 Kudos
3 Replies
JonMorris2
Occasional Contributor II

Anyone? Never mind, I've figured it out:

  // focus map

  IMap map = ...

  // new data source for table

  IFeatureWorkspace workspace = ...

  ITableCollection tableCollection = (ITableCollection)map;

  List<ITable> tables = new List<ITable>();

  for (int i = 0; i < tableCount; i++)

  {

  tables.Add(tableCollection.get_Table(i));

  }

  // iterate over a copy of the tables so we can add and remove

  foreach (ITable oldTable in tables)

  {

  IDataset dataset = (IDataset)oldTable;

  IDatasetName datasetName = (IDatasetName)dataset.FullName;

  // assumes table name is same in new data source

  // get the new table

  ITable newTable = workspace.OpenTable(datasetName);

  // replace the table in the TOC

  ITableCollection tableCollection = (ITableCollection)map;

  tableCollection.RemoveTable(oldTable);

  tableCollection.AddTable(newTable);

  // fire change event to change table

  IMapAdmin2 mapAdmin2 = (IMapAdmin2)map;

  mapAdmin2.FireChangeTable(oldTable, newTable);

  }

  // Redraw the map

  IActiveView activeView = (IActiveView)map;

  activeView.Refresh();

  // Refresh TOC

  ESRI.ArcGIS.ArcMapUI.IContentsView contentsView = ArcMap.Document.CurrentContentsView;

  contentsView.Refresh(null);

View solution in original post

Reply
0 Kudos
BBulla
by
Occasional Contributor

Hi Jon,

Thanks for posting this.  Could you also post the code you used for changing the datasource for featurelayers and raster??

Thanks,

Reply
0 Kudos
JonMorris2
Occasional Contributor II

There's a snippet here that can change feature layer data source. The key thing is to call FireChangeFeatureClass after you've made the change to make sure the map is updated.

// Change FeatureClass of layer

  featureLayer.FeatureClass = newFeatureClass;

  mapAdmin2.FireChangeFeatureClass(oldFeatureClass, newFeatureClass);

Reply
0 Kudos