I want to set certain fields of a featurelayer to be visible on startup. how would I go about changing the field visibility of certain fields?

if (featureLayer.Name == "LayerName")
    // Code to set certain fields to visible

I tried using the IFields

IFields fields = featClass.Fields;
IField field = null;
for (int i = 0; i < fields.FieldCount; i++)
    field = fields.get_Field(i);

    if (field.Name == "ObjectID")
        // field.Visible = false;

but I dont see a visibility property

UPDATE: Here is the equivalent code based on Hornbydd's answer below in C#

void SetFieldVis()
    // Get Document
    IMxDocument pMXD = ArcMap.Application.Document as IMxDocument;

    // Get Map
    IMap pMap = ArcMap.Document.FocusMap;

    // Get first layer in TOC -> in my case set up an enum and loop through the layers
    UIDClass uid = new UIDClass();
    uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
    IEnumLayer enumLayers = map.get_Layers(uid, true);
    IFeatureLayer pFeatureLayer = enumLayers.Next() as IFeatureLayer;

    while (pFeatureLayer != null)
        if (pFeatureLayer.Name == "LayerName")
            // Get a pointer to the fields
            ILayerFields pLayerFields = featureLayer as ILayerFields;

            // Get the fourth field by it's index number and set its visibility to False
            pLayerFields.FieldInfo[3].Visible = false;

Here is some VBA showing you how to change the visibility of a field for a layer loaded in ArcMap.

Public Sub SetFieldVis()
    ' Get Document
    Dim pMXD As IMxDocument
    Set pMXD = ThisDocument

    ' Get Map
    Dim pMap As IMap
    Set pMap = pMXD.FocusMap

    ' Get first layer in TOC
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = pMap.Layer(0)

    ' Get a pointer to the fields
    Dim pLayerFields As ILayerFields
    Set pLayerFields = pFeatureLayer

    ' Get the fourth field by it's index number and set its visibility to False
    Dim pFieldInfo As IFieldInfo3
    Set pFieldInfo = pLayerFields.FieldInfo(3)
    pFieldInfo.Visible = False

End Sub

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.