This document is archived and information here might be outdated.  Recommended version.


Set Data Source Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Set Data Source Snippet

How to change the datasource of a layer. It changes the data source of the first layer in the selected dataframe.

[C#]
/// <summary>
/// How to change the datasource of a layer. It changes the data source of the first layer in the selected dataframe.
/// </summary>
/// <param name="mxDocument">An IMxDocument interface.</param>
/// <param name="strDatasource">A String that is the DataSource of a Coverage. Ex: "C:\arcgis\ArcTutor\Catalog\Yellowstone"</param>
/// <param name="strFeatureClassName">A String that is the FeatureDataset name. Ex: "states"</param>
/// <param name="strFeatureDatasetName">A String that is the FeatureClass name. Ex: "arc"</param>
/// <remarks></remarks>
public void SetDataSource(ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument, System.String strDatasource, System.String strFeatureClassName, System.String strFeatureDatasetName)
{

    // Create a new ArcInfoWorkspaceFactory
    ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory=new ESRI.ArcGIS.DataSourcesFile.ArcInfoWorkspaceFactoryClass();

    // Get FeatureClass to be set as datasource
    ESRI.ArcGIS.Geodatabase.IWorkspace workspace=workspaceFactory.OpenFromFile(strDatasource, 0);

    // Cast from IWorkspace to IFeatureWorkspace
    ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace=(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;

    ESRI.ArcGIS.Geodatabase.IFeatureDataset featureDataset=featureWorkspace.OpenFeatureDataset(strFeatureClassName);
    ESRI.ArcGIS.Geodatabase.IFeatureClassContainer featureClassContainer=(ESRI.ArcGIS.Geodatabase.IFeatureClassContainer)featureDataset;
    ESRI.ArcGIS.Geodatabase.IFeatureClass newFeatureClass=featureClassContainer.get_ClassByName(strFeatureDatasetName);

    // Get the focus map
    ESRI.ArcGIS.Carto.IMap map=mxDocument.FocusMap;

    // Cast from IMap to IActiveView
    ESRI.ArcGIS.Carto.IActiveView activeView=(ESRI.ArcGIS.Carto.IActiveView)map;

    // Cast from IMap to IMapAdmin2
    ESRI.ArcGIS.Carto.IMapAdmin2 mapAdmin2=(ESRI.ArcGIS.Carto.IMapAdmin2)map;

    // Get the first layer in the map
    ESRI.ArcGIS.Carto.ILayer layer=map.get_Layer(0);

    ESRI.ArcGIS.Carto.IFeatureLayer featureLayer=(ESRI.ArcGIS.Carto.IFeatureLayer)layer;
    ESRI.ArcGIS.Geodatabase.IFeatureClass oldFeatureClass=featureLayer.FeatureClass;

    // Change FeatureClass of layer
    featureLayer.FeatureClass=newFeatureClass;
    mapAdmin2.FireChangeFeatureClass(oldFeatureClass, newFeatureClass);

    // Redraw the map
    activeView.Refresh();

    // Update and Refresh TOC
    ESRI.ArcGIS.Geodatabase.IFeatureDataset newFeatureDataset=(ESRI.ArcGIS.Geodatabase.IFeatureDataset)featureClassContainer;
    featureLayer.Name=newFeatureDataset.Name + " " + newFeatureClass.AliasName;
    ESRI.ArcGIS.ArcMapUI.IContentsView contentsView=mxDocument.CurrentContentsView;
    contentsView.Refresh(null);

}
[Visual Basic .NET]
''' <summary>
''' How to change the datasource of a layer. It changes the data source of the first layer in the selected dataframe.
''' </summary>
''' <param name="mxDocument">An IMxDocument interface.</param>
''' <param name="strDatasource">A String that is the DataSource of a Coverage. Ex: "C:\arcgis\ArcTutor\Catalog\Yellowstone"</param>
''' <param name="strFeatureClassName">A String that is the FeatureDataset name. Ex: "states"</param>
''' <param name="strFeatureDatasetName">A String that is the FeatureClass name. Ex: "arc"</param>
''' <remarks></remarks>
Public Sub SetDataSource(ByVal mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument, ByVal strDatasource As System.String, ByVal strFeatureClassName As System.String, ByVal strFeatureDatasetName As System.String)

  ' Create a new ArcInfoWorkspaceFactory
  Dim workspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory=New ESRI.ArcGIS.DataSourcesFile.ArcInfoWorkspaceFactoryClass

  ' Get FeatureClass to be set as datasource
  Dim workspace As ESRI.ArcGIS.Geodatabase.IWorkspace=workspaceFactory.OpenFromFile(strDatasource, 0)

  ' Cast from IWorkspace to IFeatureWorkspace
  Dim featureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace=CType(workspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)

  Dim featureDataset As ESRI.ArcGIS.Geodatabase.IFeatureDataset=featureWorkspace.OpenFeatureDataset(strFeatureClassName)
  Dim featureClassContainer As ESRI.ArcGIS.Geodatabase.IFeatureClassContainer=CType(featureDataset, ESRI.ArcGIS.Geodatabase.IFeatureClassContainer)
  Dim newFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass=featureClassContainer.ClassByName(strFeatureDatasetName)

  ' Get the focus map
  Dim map As ESRI.ArcGIS.Carto.IMap=mxDocument.FocusMap

  ' Cast from IMap to IActiveView
  Dim activeView As ESRI.ArcGIS.Carto.IActiveView=CType(map, ESRI.ArcGIS.Carto.IActiveView)

  ' Cast from IMap to IMapAdmin2
  Dim mapAdmin2 As ESRI.ArcGIS.Carto.IMapAdmin2=CType(map, ESRI.ArcGIS.Carto.IMapAdmin2)

  ' Get the first layer in the map
  Dim layer As ESRI.ArcGIS.Carto.ILayer=map.Layer(0)

  Dim featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer=CType(layer, ESRI.ArcGIS.Carto.IFeatureLayer)
  Dim oldFeatureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass=featureLayer.FeatureClass

  ' Change FeatureClass of layer
  featureLayer.FeatureClass=newFeatureClass
  mapAdmin2.FireChangeFeatureClass(oldFeatureClass, newFeatureClass)

  ' Redraw the map
  activeView.Refresh()

  ' Update and Refresh TOC
  Dim newFeatureDataset As ESRI.ArcGIS.Geodatabase.IFeatureDataset=CType(featureClassContainer, ESRI.ArcGIS.Geodatabase.IFeatureDataset)
  featureLayer.Name=newFeatureDataset.Name + " " + newFeatureClass.AliasName
  Dim contentsView As ESRI.ArcGIS.ArcMapUI.IContentsView=mxDocument.CurrentContentsView
  contentsView.Refresh(Nothing)

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.ArcMapUI
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.DataSourcesFile
  • ESRI.ArcGIS.Display
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System