【ArcEngine 10 二次开发】DataGridView显示Layer中的属性表

时间:2022-05-20 14:46:08

显示图层Layer中的属性表

新建一个Form窗口

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 AttributesTableForm2 : Form
{

private ILayer m_Layer;

public AttributesTableForm2(ILayer pMapLayer)
{
InitializeComponent();
m_Layer= pMapLayer;
}

private void AttributesTableForm2_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;
}
}
}