DataGridView中显示shp图层数据

1. 简单点查询

(1)引用

    

   	using ESRI.ArcGIS.Geodatabase;
	using ESRI.ArcGIS.DataSourcesFile;
	using ESRI.ArcGIS.esriSystem;
	using ESRI.ArcGIS.Geometry;

 
(2)ArcEngine的Licence认证

IAoInitialize m_AoInitialize = new AoInitializeClass();
     esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;
     licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

 

(3)空间查询或属性查询

OpenFileDialog ofdDBF = new OpenFileDialog();
ofdDBF.Filter = "属性表文件(*.dbf)|*.dbf";
if (ofdDBF.ShowDialog() == DialogResult.OK)
{
    string OpenFileName = ofdDBF.FileName.Trim();
    string dbfFilePath = Path.GetDirectoryName(OpenFileName);
    string dbfFileName = Path.GetFileName(OpenFileName);

    IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
    IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(dbfFilePath, 0);
    IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
    if (pFeatureWorkspace != null)
    {
        IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(dbfFileName);
        if (pFeatureClass != null)
        {
            DataTable dt = new DataTable();
            DataColumn dc = null;

            for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
            {
                dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name);
                dt.Columns.Add(dc);
            }

            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCursor.NextFeature();
            DataRow dr = null;
            while (pFeature != null)
            {
                dr = dt.NewRow();
                for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
                {
                    if (pFeatureClass.FindField(pFeatureClass.ShapeFieldName) == j)
                    {
                        dr[j] = pFeatureClass.ShapeType.ToString();
                    }
                    else
                    {
                        dr[j] = pFeature.get_Value(j).ToString();
                    }
                }
                dt.Rows.Add(dr);
                pFeature = pFeatureCursor.NextFeature();
            }
            dataGridView1.DataSource = dt;
        }
    }
}

 

你可能感兴趣的:(J#)