如何将ESRI IFeatureLayer 转为c# DataGridView

 在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;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值