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();  
            } 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jingss_3/archive/2010/01/04/5127951.aspx

你可能感兴趣的:(ArcEngine)