[转载]地址:http://www.cnblogs.com/joysky/p/4581208.html
有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求。
按下面介绍的方法可轻松实现图层属性过滤显示:
1、当图层已经加载时
private void ShowByFilter(AxMapControl sMapCtr, IFeatureLayer sFlyr, string sFilter) { ESRI.ArcGIS.Carto.IFeatureLayerDefinition pDef = (ESRI.ArcGIS.Carto.IFeatureLayerDefinition)sFlyr; pDef.DefinitionExpression = sFilter; sMapCtr.ActiveView.Refresh(); }
说明: sFilter 语句与SQL语句略有不同,使用时需要注意!
2、当图层尚未加载时
private IFeatureClass GetFeatureClass(IWorkspace pWS, string sTabName, string sAttrFilter) { try { if (pWS == null || sTabName == null) return null; IFeatureWorkspace pFWS = pWS as IFeatureWorkspace; IFeatureClass pFC = null; pFC = pFWS.OpenFeatureClass(sTabName); IQueryDef pQueryDef = pFWS.CreateQueryDef(); pQueryDef.Tables = sTabName; pQueryDef.WhereClause = sAttrFilter; IQueryName2 pQueryName = new FeatureQueryNameClass(); pQueryName.PrimaryKey = pFC.OIDFieldName; pQueryName.QueryDef = pQueryDef; IDataset pDS = (IDataset)pFWS; IWorkspaceName pWSName = (IWorkspaceName)pDS.FullName; IDatasetName pDSName = (IDatasetName)pQueryName; pDSName.WorkspaceName = pWSName; IName pName = (IName)pQueryName; pFC = pName.Open() as IFeatureClass; return pFC; } catch { return null; } }
其他补充:
参照其他博客: