loading fields from a GIS file into a combo box

2386
2
06-13-2016 05:57 PM
Highlighted
New Contributor II

hello,

Using c# I have an open dialog object in my form where the user selects an input GIS file.  Once a file is selected, I have a combo box object that I want to load all the field names from that input GIS file into so the user can select the required field for analysis.

How would I go about doing that?

Thank you.

Reply
0 Kudos
2 Replies
Highlighted
New Contributor

Hi,

This can be achieved. I have created an AddIn, on click of this AddIn the below form opens up.Mform.JPG

You need to click the browse button, this will open the AddData command of the ArcMap.AddData.JPG After which one needs to select the desired feature class, this will display the full path of the selected feature class (Also it will add the feature class to ArcMap) in the text box.

Now, The ObjectID of the selected feature class will be populated in the combo box.

SampleOut.jpg

Below is the code.

IDocument d = ArcMap.Document as IDocument;

            IUID ud = new UIDClass();

            ud.Value = "{E1F29C6B-4E6B-11D2-AE2C-080009EC732A}";

            ICommandItem ci = d.CommandBars.Find(ud);

            ci.Execute();

            IMxDocument pMxDocument = (IMxDocument)ArcMap.Application.Document;

            IMap pMap = pMxDocument.FocusMap;

            ILayer pLayer = pMap.get_Layer(0);

            ESRI.ArcGIS.Geodatabase.IDataset dataset = (ESRI.ArcGIS.Geodatabase.IDataset)(pLayer); // Explicit Cast

            textBox1.Text= (dataset.Workspace.PathName + "\\" + dataset.Name);

        

                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");

                IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);

                IWorkspace workspace = workspaceFactory.OpenFromFile(dataset.Workspace.PathName, 0);

                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;

                ITable table = featureWorkspace.OpenTable(dataset.BrowseName);

                ICursor cursor = (ICursor)table.Search(null, true);

                IRow row = cursor.NextRow();

                int field = table.Fields.FindField("OBJECTID");

                //clear the combobox

                comboBox1.Items.Clear();

                //then populate the combo boxes

                while (row != null)

                {

                    string strValue = Convert.ToString(row.get_Value(field));

                    if (comboBox1.Items.Contains(strValue) == false)

                        comboBox1.Items.Add(strValue);

                    row = cursor.NextRow();

                }

Reply
0 Kudos
Highlighted
MVP Honored Contributor

Once you have the IFeatureClass (pFClass in this example) that the user has selected, simply loop through its fields and add them to your combo box. Here's the way I've done that in one of my applications. It's written in VB, but you shouldn't have a problem in converting it. I'm only adding the Field Types 0-4 which represent numeric and text fields.

Dim pFields As ESRI.ArcGIS.Geodatabase.IFields2

pFields = pFClass.Fields

For i As Integer = 0 To pFields.FieldCount - 1

    If pFields.Field(i).Type < 5 Then comboBox.Items.Add(pFields.Field(i).Name)

Next

Reply
0 Kudos