前阵子写一个到处SHP文件的函数不断出现到处后属性为空的问题,后来尝试半天,发现是少了两行代码,虽然解决了,但是其实还是不知道这两行代码是干什么的,后来网上读到前人帖子提到这个。http://blog.csdn.net/brokencar/article/details/6069997
API中的REMARK,他是这样写的:When this interface is used to access features from a map's feature selection (IMap.FeatureSelection), the IEnumFeatureSetup.AllFields property should be used to return fully hydrated features.也就是说:ArcGIS中FeatureSelection默认的时候只存入Feature 的Shape,而不是整个Feature的字段数据。如果要查看其他数据,必须要进行以下转换才可以。
IActiveView pActiveView = axMapControl1.ActiveView;
IMap pMap = axMapControl1.ActiveView.FocusMap;if (pMap.SelectionCount == 0) return;
IEnumFeature pEnumFeature = pMap.FeatureSelection as IEnumFeature;
IEnumFeatureSetup pEnumFeatureSetup = pEnumFeature as IEnumFeatureSetup;
pEnumFeatureSetup.AllFields = true;
IFeature pFeature = pEnumFeature.Next();
while (pFeature != null)
{
for (int i = 0; i < frmExport.pTableField.Rows.Count; i++)
{
//如果缺少红色的两行代码,则虽然pFeature不为空,pFeature的fieldcount也是对的,但是 pFeature.get_Value(i)时一直为null,导出属性表很多字段也会为空
object strtemp = pFeature.get_Value(i);
string str1;
if(strtemp!=null)
str1 = strtemp.ToString();
}
}