C# Retrieving field data type from a FeatureLayer/Class

3310
5
Jump to solution
03-26-2018 06:33 AM
MikeRatcliffe
Occasional Contributor

I want to parse user input through a field data type converter (if...else if) so that I don't get a "Data Type Mismatch" from my QueryFilter/SpatialQueryFilter.  However, I'm having trouble retrieving the actual field data type from the FeatureLayer or FeatureClass.  I can get to the Field name for the query, but not the data type (Text, Int, Double, Long, etc...).

0 Kudos
1 Solution

Accepted Solutions
MikeRatcliffe
Occasional Contributor

This is where I ended up...

`

public async void MnlSrchCmbobx_SelectionChangeCommitted(object sender, EventArgs e)
{
var attfn = MnlSrchCmbobx.Text;    //string choice from combobox (search field)
Form1.GVar.ms_attfn = attfn;          //string to global variable  

await ActMap();                               //active map checker

Map map = MapView.Active.Map;

var srch_lyr = map.FindLayers(lyr).FirstOrDefault() as FeatureLayer;       //layer locator from "lyr" in activemap TOC


await QueuedTask.Run(() =>
{

TableDefinition srch_FCD = srch_lyr.GetTable().GetDefinition();      //table definition of featurelayer

var Find_Fld = srch_FCD.FindField(attfn);                                      //find the search field index

var Get_Flds = srch_FCD.GetFields();                                          //get the readonly collection of fields from the table

Field ms_field = Get_Flds.ElementAt(Find_Fld);                         //Field object: via search field index from collection

var FldTp = ms_field.FieldType.ToString();                                 //get fieldtype as a string

Form1.GVar.ms_v_datatype = FldTp;                                       //assign fieldtype to global variable

});

AsetSrchFldDTLbl.Text = Form1.GVar.ms_v_datatype;        //test output on sub-form to a label.
}

Thanks, Rich and Uma.

View solution in original post

0 Kudos
5 Replies
RichRuh
Esri Regular Contributor

Given the FeatureClass, you can get a FeatureClassDefinition object by calling FeatureClass.GetDefinition().

From the FeatureClassDefinition you can call FindField() to find the field index, given the name.  With that and a fields collection from FeatureClassDefinition.GetFields(), you can get a Field object.  

Finally, the Field object has a FieldType property.

0 Kudos
MikeRatcliffe
Occasional Contributor

Thanks for pointing to FeatureClassDefinition, Rich.

I'm still fuzzy on acquiring the Field object...

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Mike,

Check out the Renderer community sample.  There is code in there that gets a feature layer's numeric field.

In particular, check out the following 2 methods:

Given a feature layer, this method get's the first numeric field.

internal static string GetNumericField(FeatureLayer featureLayer)
        {
            //Get all the visible numeric fields for the layer.
            var numericField = featureLayer.GetFieldDescriptions().FirstOrDefault(f => IsNumericFieldType(f.Type) && f.IsVisible);
            return numericField.Name;
        }

This is the helper method that does the work:

 internal static bool IsNumericFieldType(FieldType type)
        {
            switch (type)
            {
                case FieldType.Double:
                case FieldType.Integer:
                case FieldType.Single:
                case FieldType.SmallInteger:
                    return true;
                default:
                    return false;
            }
        }

Thanks

Uma

0 Kudos
MikeRatcliffe
Occasional Contributor

Thanks, Uma.

That isn't entirely what I'm after.

I'm collecting from a sub-form user input (string) & fieldname (combobox) from a featurelayer/class as variables for a queryfilter/spatialqueryfilter on the main-form.  I need to know the fieldname's FIELDTYPE to parse appropriately for the query filter.  Other wise I get the "data type mismatch in criteria expression."

I'm looking for the FIELDTYPE of a selected column.

I followed Rich's lead from above, but am troubleshooting the FIELD object, so I can get the correct FIELDTYPE.

thanks.

0 Kudos
MikeRatcliffe
Occasional Contributor

This is where I ended up...

`

public async void MnlSrchCmbobx_SelectionChangeCommitted(object sender, EventArgs e)
{
var attfn = MnlSrchCmbobx.Text;    //string choice from combobox (search field)
Form1.GVar.ms_attfn = attfn;          //string to global variable  

await ActMap();                               //active map checker

Map map = MapView.Active.Map;

var srch_lyr = map.FindLayers(lyr).FirstOrDefault() as FeatureLayer;       //layer locator from "lyr" in activemap TOC


await QueuedTask.Run(() =>
{

TableDefinition srch_FCD = srch_lyr.GetTable().GetDefinition();      //table definition of featurelayer

var Find_Fld = srch_FCD.FindField(attfn);                                      //find the search field index

var Get_Flds = srch_FCD.GetFields();                                          //get the readonly collection of fields from the table

Field ms_field = Get_Flds.ElementAt(Find_Fld);                         //Field object: via search field index from collection

var FldTp = ms_field.FieldType.ToString();                                 //get fieldtype as a string

Form1.GVar.ms_v_datatype = FldTp;                                       //assign fieldtype to global variable

});

AsetSrchFldDTLbl.Text = Form1.GVar.ms_v_datatype;        //test output on sub-form to a label.
}

Thanks, Rich and Uma.

0 Kudos