Cross post from: https://geonet.esri.com/thread/125010

Does anyone know how to change the data source of a table using arcobjects? I know it can be done with arcpy, but I'm using C#. I've managed to change the source of all the layers in the mxd, but I need to do tables too.

I've come up with a solution by adding and removing from the table collection.

Is this the best way to do it?

        IMap map = ...;
        IWorkspace = ...;

        ITableCollection tableCollection = (ITableCollection)map;
        int tableCount = tableCollection.TableCount;

        // iterate over a copy of the tables so we can add and remove
        List<ITable> tables = new List<ITable>();
        for (int i = 0; i < tableCount; i++)

        foreach (ITable oldTable in tables)
            IDataset dataset = (IDataset)oldTable;
            IDatasetName datasetName = (IDatasetName)dataset.FullName;
            string workspaceName = datasetName.WorkspaceName.PathName;

            // get the new table
            ITable newTable = ((IFeatureWorkspace)workspace).OpenTable(datasetName.Name);

            // replace the table in the TOC

            // fire change event to change table
            IMapAdmin2 mapAdmin2 = (IMapAdmin2)map;
            mapAdmin2.FireChangeTable(oldTable, newTable);

Note: this is simplified - I did check the table was in the new workspace before opening it.


You should use the table property of IStandaloneTable to change its datasource. First create a reference to the new ITable (e.g. a table from a geodatabase), then use this code to replace the Table property:

            IMap map = ...;
            IStandaloneTableCollection tbcol= (IStandaloneTableCollection)map;

            for (int i = 0; i < tbcol.StandaloneTableCount; i++)
                var stTable = tbcol.get_StandaloneTable(i);
                stTable.Table = newTable; //Here put the new ITable  To change the datasource
                    if (!stTable.Valid)
                        MessageBox.Show("Table is not valid, "+stTable.Name);
  • Thanks Farid - I came up with a solution using ITableCollection (see edit). Is there a reason for using IStandaloneTableCollection instead?
    – jon_two
    Mar 13 '15 at 14:27
  • I don't see any differences here. But IStandaloneTableCollection is implemented with NALayer (esriNetworkAnalyst) class too. Meaning this is more complete that ITableCollection Mar 13 '15 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.