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 addshp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private AxMapControl m_MapCtl;
public Form2(AxMapControl pMapCtl)
{
InitializeComponent();
m_MapCtl = pMapCtl;
}
//private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
//{
//}
private void Form2_Load(object sender, EventArgs e)
{
ILayer PLayer = m_MapCtl.get_Layer(0);
IFeatureLayer pFLay = PLayer as IFeatureLayer;
IFeatureClass pFC = pFLay.FeatureClass;
//关联图层和要素类
//shp 的拆箱过程(自定义)
IFeatureCursor pFcursor = pFC.Search(null, false);
IFeature pFeature = pFcursor.NextFeature();
//定义属性表一个指针
//指针指向第一条记录
DataTable pTable = new DataTable();
DataColumn colName = new DataColumn("Name");
colName.DataType = System.Type.GetType("System.String");
pTable.Columns.Add(colName);
//创建Column1(字段名、属性)
DataTable pTable2 = new DataTable();
DataColumn colName2 = new DataColumn("Area");
colName.DataType = System.Type.GetType("System.String");
pTable.Columns.Add(colName2);
//创建Column2(字段名、属性)
int indexofName = pFC.FindField("Name");
int indexofName2 = pFC.FindField("Area");
while (pFeature != null)
{
string name = pFeature.get_Value(indexofName).ToString();
string name2 = pFeature.get_Value(indexofName2).ToString();
DataRow pRow = pTable.NewRow();
pRow[0] = name;
pRow[1] = name2;
pTable.Rows.Add(pRow);
//新建行
//装入数据
//完成装载,把新行Add入数据表
pFeature = pFcursor.NextFeature();
//指针下移,直到遍历所有记录
}
dataGridView1.DataSource = pTable;
//在dataGridView中显示pTable
}
}
}
转载于AE入门基础教程示例