ArcEngine中对已经存在的数据表格添加字段(转载)

下例中是对mapControl中当前地图添加“name_city”字段,主要用到IField,IFieldEdit,ITable(IClass),IFeatureLayer,IFeatureClass,IFeature字段,其中IField,IFieldEdit是创建新的字段“name_city”,每个要素的“name_city”字段存储的都是“city_name”。

注意:在调用AddField方法时,利用ITable或者IClass,而不能使用IFieldsEdit,参考AE的帮助文档:

The IFieldsEdit interface is used when creating a fields collection. You cannot use it to insert or delete a field from a fields collection belonging to an existing table. To add a field to an existing object class, use the IClass::AddField method.  To remove a field from an existing object class, use the IClass::DeleteField method.

从上面,我们知道IFieldsEdit 在创建新的数据表格时起作用,而要插入或者删除当前已经存在的数据表时,利用IClass。ITable继承于IClass,因此也可以使用ITable。

view plaincopy to clipboardprint?
//new a field and add to the first layer in the map  
            //new a field: "name_cit", type:string  
            IField pField = new FieldClass();  
            IFieldEdit pFieldEdit = pField as IFieldEdit;  
            pFieldEdit.Name_2 = "name_city";  
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;  
            //achieve the first layer in the map  
            IFeatureLayer pFeatureLayer = axMapControl1.Map.get_Layer(0) as IFeatureLayer;  
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;  
            IClass pTable = pFeatureClass as IClass;        //use ITable or IClass  
            pTable.AddField(pFieldEdit);  
            //set values of every feature's field-"name_cit" in the first layer  
            for (int i = 0; i < pFeatureClass.FeatureCount(null); i++)  
            {  
                IFeature pFeature = pFeatureClass.GetFeature(i);  
                pFeature.set_Value(pFeature.Fields.FindField("name_city"), "city_name");  
                pFeature.Store();  
            } 

转载于:https://www.cnblogs.com/linhugh/articles/2520886.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值