红色的冰Oo 2023-03-28 09:26 采纳率: 57.1%
浏览 77
已结题

C#ArcEngine二次开发引入类似Arcmap内属性表内容的操作

请教 C#ArcEngine二次开发引入类似ArcMap对属性表内容操作的方法
通过其他博主的帮助,已经实现了属性表的生成

img

但是想要增加类似ArcMap对属性表内容的操作,比如缩放至所选项、增删改查等操作。最好可以基于下列代码进行功能添加

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
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;

namespace AutoPolyLine
{
    public partial class AttributeTable : Form
    {
        ILayer mLayer; //**
        private IFeatureLayer currentFeatureLayer;

        public IFeatureLayer CurrentFeatureLayer
        {
            get { return currentFeatureLayer; }
            set { currentFeatureLayer = value; }
        }
        //获取主窗体传入的地图
        private IMap currentMap;

        public IMap CurrentMap
        {
            get { return currentMap; }
            set { currentMap = value; }
        }
        public AttributeTable(ILayer pLayerT) //**
        {
            InitializeComponent();

            mLayer = pLayerT; //**
            OpenAttributeTable();
        }
        private void AttributeTable_Load(object sender, EventArgs e)
        {
            OpenAttributeTable(); //自定义方法            
        }

        public void OpenAttributeTable()
        {
            IFeatureLayer pFeatureLayer = mLayer as IFeatureLayer; //**
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; //** using ESRI.ArcGIS.Geodatabase;

            DataTable dt = new DataTable();
            if (pFeatureClass != null)
            {
                DataColumn dc;
                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;
                while (pFeature != null)
                {
                    dr = dt.NewRow();
                    for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
                    {
                        //判断feature的形状
                        if (pFeature.Fields.get_Field(j).Name == "Shape")
                        {
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                            {
                                dr[j] = "点";
                            }
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
                            {
                                dr[j] = "线";
                            }
                            if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
                            {
                                dr[j] = "面";
                            }
                        }
                        else
                        {
                            dr[j] = pFeature.get_Value(j).ToString();//增加行
                        }
                    }
                    dt.Rows.Add(dr);
                    pFeature = pFeatureCursor.NextFeature();
                }
            }
            this.dataGridView1.DataSource = dt;
        }
    }

}

  • 写回答

3条回答 默认 最新

  • 关注

    基于最新版ChatGPT4的回答,望采纳!!!有其他问题也可以询问我哦、”(最新版更智能,功能更加强大)

    为了实现缩放至所选项、增删改查等操作,您可以根据以下代码进行功能添加:

    using ESRI.ArcGIS.Carto;
    using ESRI.ArcGIS.Geodatabase;
    using ESRI.ArcGIS.Geometry;
    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;
    
    namespace AutoPolyLine
    {
        public partial class AttributeTable : Form
        {
            ILayer mLayer;
            private IFeatureLayer currentFeatureLayer;
            private IMap currentMap;
            private IActiveView activeView;
            private DataTable dataTable;
    
            public IFeatureLayer CurrentFeatureLayer
            {
                get { return currentFeatureLayer; }
                set { currentFeatureLayer = value; }
            }
    
            public IMap CurrentMap
            {
                get { return currentMap; }
                set { currentMap = value; }
            }
    
            public AttributeTable(ILayer pLayerT, IMap pMap)
            {
                InitializeComponent();
    
                mLayer = pLayerT;
                currentMap = pMap;
                activeView = currentMap as IActiveView;
                OpenAttributeTable();
            }
    
            private void AttributeTable_Load(object sender, EventArgs e)
            {
                OpenAttributeTable();
            }
    
            public void OpenAttributeTable()
            {
                // ... 保留原有代码 ...
    
                this.dataGridView1.DataSource= dt;
    dataTable = dt;
    }
    
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            ZoomToSelectedFeatures();
        }
    
        private void ZoomToSelectedFeatures()
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridView1.SelectedRows;
            if (selectedRows.Count > 0)
            {
                IFeatureLayer pFeatureLayer = mLayer as IFeatureLayer;
                IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
    
                IEnvelope envelope = new EnvelopeClass();
                envelope.SetEmpty();
    
                foreach (DataGridViewRow row in selectedRows)
                {
                    int objectId = Convert.ToInt32(row.Cells["OBJECTID"].Value);
                    IFeature feature = pFeatureClass.GetFeature(objectId);
                    envelope.Union(feature.Shape.Envelope);
                }
    
                if (!envelope.IsEmpty)
                {
                    envelope.Expand(1.2, 1.2, true);
                    activeView.Extent = envelope;
                    activeView.Refresh();
                }
            }
        }
    
        // 在此添加增删改查的相关操作方法
    }
    }
    

    这段代码中,我添加了一个ZoomToSelectedFeatures方法,用于缩放至所选项。同时,我修改了AttributeTable构造函数,使其接收IMap参数,并在ZoomToSelectedFeatures方法中使用此参数进行缩放操作。

    此外,您还可以根据需要添加增删改查的相关操作方法。在dataGridView1的事件处理程序中调用这些方法,以便在用户与属性表交互时执行相应操作。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月12日
  • 已采纳回答 4月4日
  • 赞助了问题酬金15元 3月28日
  • 创建了问题 3月28日

悬赏问题

  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值