如何将ESRI IFeatureLayer 转为c# DataGridView

    科技2025-04-14  12

     在ArcGIS Engine开发中经常需要将ESRI IFeatureLayer 转为c# DataGridView来显示属性表。以下是转换例子代码。更多 ArcGIS Engine开发相关讨论请加QQ群722805168,请注明“ArcGIS Engine开发”。

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; namespace ArcTest { public partial class AttributesTableForm : Form { private ILayer m_Layer; public AttributesTableForm(ILayer pMapLayer) { InitializeComponent(); m_Layer= pMapLayer; } private void AttributesTableForm_Load(object sender, EventArgs e) { ILayer pLayer = m_Layer; IFeatureLayer pFLayer = pLayer as IFeatureLayer; IFeatureClass pFC = pFLayer.FeatureClass; IFeatureCursor pFCursor = pFC.Search(null, false); IFeature pFeature = pFCursor.NextFeature(); DataTable pTable = new DataTable(); //添加自定义字段 DataColumn colName = new DataColumn("省 直辖市"); colName.DataType = System.Type.GetType("System.String"); pTable.Columns.Add(colName); //添加自定义字段 DataColumn colArea = new DataColumn("面积"); colArea.DataType = System.Type.GetType("System.Double"); pTable.Columns.Add(colArea); int indexOfName = pFC.FindField("CHINESE"); int indexOfArea = pFC.FindField("Area"); while(pFeature != null) { string name = pFeature.get_Value(indexOfName).ToString(); double area = (double)pFeature.get_Value(indexOfArea); DataRow pRow = pTable.NewRow(); pRow[0] = name; pRow[1] = area; pTable.Rows.Add(pRow); pFeature = pFCursor.NextFeature(); } dataGridView1.DataSource = pTable; } } }

     

    Processed: 0.009, SQL: 8