In the following example, the "name_city" field is added to the current map in mapControl. IField, IFieldEdit, ITable (IClass), IFeatureLayer, IFeatureClass, and IFeature fields are used. IField, IFieldEdit is used to create a new field "name_city". The "name_city" Field of each element is stored as "city_name ".
Note: when calling the AddField method, use ITable or IClass instead of IFieldsEdit. Refer to the help documentation of 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.
From the above, we know that IFieldsEdit works when creating a new data table, and IClass is used to insert or delete an existing data table. ITable inherits from IClass, so you can also use 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 ();
}
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/jingss_3/archive/2010/01/04/5127951.aspx