Add fields to an existing data table in ArcEngine

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.