ArcGIS Engine创建字段,并为字段赋值

 1、定义“新建字段”的方法,形参可以根据自己的需要进行修改,比如可以添加字段的精度和小数位数等,以下为C#“新建字段”的代码。

//新建字段
public void AddField(IFeatureClass pFeatureClass, string name, string aliasName,esriFieldType FieldType, bool n)
        {
            //判断字段是否存在
            if (pFeatureClass.Fields.FindField(Name) > -1) return;
            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Name_2 = name;  //字段名称
            pFieldEdit.AliasName_2 = aliasName;  //字段别名
            pFieldEdit.Type_2 = FieldType;  //字段类型
            pFieldEdit.IsNullable_2 = n;    //字段是否为空
            IClass pClass = pFeatureClass as IClass;
            pClass.AddField(pField);
        }

2、在所需要的位置调用该方法,以下是在button控件中的调用。

//获取图层
IFeatureLayer pFeatureLayer = axMapControl.get_Layer(0) as IFeatureLayer; 
//添加字段
AddField(pFeatureLayer.FeatureClass, "height", "height", esriFieldType.esriFieldTypeSingle, false);

3、为新建的字段赋值。

IFeature pFeature = null;
//赋值,m_value是将得到的结果传入height字段中
pFeature.set_Value(pFeature.Fields.FindField("height"), m_value); 
//保存字段
pFeature.Store();

具体的使用情况可以根据自己的需求参考。

你可能感兴趣的:(GIS二次开发,c#,gis,arcgis)