Hii, i am trying to display Field names and field count. here is my code but i am not getting result. Please help me
IMxDocument pmx = m_application.Document as IMxDocument;
IMap pmap = pmx.FocusMap;
ILayer layer = pmap.get_Layer(0);
IFeatureLayer pflayer = (IFeatureLayer)layer;
IFeatureClass fc = pflayer.FeatureClass;
IFields fields = fc.Fields;
int fieldCount = fields.FieldCount;
int endFieldIndex = fieldCount - 1;
int fieldIndex = 0;
while (fieldIndex <= endFieldIndex)
{
IField field = fields.get_Field(fieldIndex);
Console.WriteLine("The field at field index {0} is named (1)", fieldIndex, field.Name);
//MessageBox.Show("The field at field index {0} is named (1)", fieldIndex.ToString(), field.Name);
fieldIndex++;
Your logic looks correct to me. If you step through your code could you verify that fc.Fields is returning values?
I wrote up a quick console application to test this and everything works fine for me. I've pasted my logic below if you want to give it a shot.
using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.esriSystem; using System; using System.Windows.Forms; namespace ListFields { class Program { [STAThread] static void Main(string[] args) { ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); IAoInitialize aoInit = new AoInitializeClass(); aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeAdvanced); var mxdPath = BrowseFolderForFile("Please select a *.mxd file", "Map Document (*.mxd)|*.mxd"); if (string.IsNullOrEmpty(mxdPath)) throw new ArgumentException("Invalid MXD Path"); var mxd = new MapDocumentClass(); mxd.Open(mxdPath, string.Empty); var uid = new UIDClass { Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}" }; for (int i = 0; i < mxd.MapCount; i++) { var map = mxd.Map; var enumLayer = map.Layers[uid, true]; enumLayer.Reset(); ILayer layer; while ((layer = enumLayer.Next()) != null) { var featClass = (layer as IFeatureLayer).FeatureClass; Console.WriteLine("Layer {0} has {1} fields.", layer.Name, featClass.Fields.FieldCount); for (int j = 0; j < featClass.Fields.FieldCount; j++) Console.WriteLine("...{0:00}. {1}", j, featClass.Fields.Field.Name); } } Console.Write("\nPress enter to exit..."); Console.ReadLine(); } static string BrowseFolderForFile(string title, string extension) { OpenFileDialog openFileDialog = new OpenFileDialog { Filter = extension, Multiselect = false, ShowHelp = true, Title = title }; return openFileDialog.ShowDialog() == DialogResult.OK ? openFileDialog.FileName : null; } } }