zoukankan      html  css  js  c++  java
  • 对已有IFields补充添加Field

    原博客【传送门

    现在需要对已有的要素图层添加字段,尝试以下代码:

    IFeatureClass pFc= ((IFeatureLayer)lyrRec).FeatureClass;          
    IFieldsEdit fldsE = pFc.Fields as IFieldsEdit;
    IField fld = new FieldClass();
    IFieldEdit2 fldE = fld as IFieldEdit2;
    fldE.Type_2 = esriFieldType.esriFieldTypeString;
    fldE.Name_2 = "XXX";
    ic.AddField(fld);
    int idTest = gpsFc.Fields.FindField("XXX");
     
    调试跟踪,发现IdTest不等于-1 说明执行成功,但是再次启动发现字段未成功添加到要素集

    查看帮助 ,对已存在的要素集添加字段 需要用到IClass

    IFeatureClass pFc= ((IFeatureLayer)lyrRec).FeatureClass;
    IClass pClass=pFc as IClass;

    IFieldsEdit fldsE = pFc.Fields as IFieldsEdit;
    IField fld = new FieldClass();
    IFieldEdit2 fldE = fld as IFieldEdit2;
    fldE.Type_2 = esriFieldType.esriFieldTypeString;
    fldE.Name_2 = "XXX";
    pClass.AddField(fld);

    再次执行 成功!

    AddField is used when creating a fields collection and cannot be used to insert a field into a fields collection belonging to an existing table. To add a field to an existing object class, use the IClass::AddField method.
    意思就是说如果修改的是当前已经存在的表或者要数类的字段,那么你就使用IClass::AddField方法生成字段
    新建要素集则不会出现这种情况

  • 相关阅读:
    linux常用统计命令
    linux文件处理命令
    linux三剑客和管道使用
    bash编程语法
    第八章:用通配符进行过滤
    第七章:数据过滤
    第六章:过滤数据
    第五章:排序检索数据
    第四章:检索数据
    第二章:MYSQL简介
  • 原文地址:https://www.cnblogs.com/marvelousone/p/7680257.html
Copyright © 2011-2022 走看看