AE属性表操作(2)

转载 2013年12月03日 11:59:00

转自:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=129398


6、选中行高亮在地图中显示

/// <summary>
        /// 该行选中时高亮显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gdvAttribute_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            IQueryFilter pQuery = new QueryFilterClass();
            int count = this.gdvAttribute.SelectedRows.Count;
            string val;
            string col;
            col = this.gdvAttribute.Columns[0].Name;
            //当只选中一行时
            if (count == 1)
            {
                val = this.gdvAttribute.SelectedRows[0].Cells[col].Value.ToString();
                //设置高亮要素的查询条件
                pQuery.WhereClause = col + "=" + val;
            }
            else//当选中多行时
            {
                int i;
                string str;
                for (i = 0; i < count - 1; i++)
                {
                    val = this.gdvAttribute.SelectedRows[i].Cells[col].Value.ToString();
                    str = col + "=" + val + " OR ";
                    pQuery.WhereClause += str;
                }
                //添加最后一个要素的条件
                val = this.gdvAttribute.SelectedRows[i].Cells[col].Value.ToString();
                str = col + "=" + val;
                pQuery.WhereClause += str;
            }
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            IFeatureSelection pFeatSelection;
            pFeatSelection = pFLayer as IFeatureSelection;
            pFeatSelection.SelectFeatures(pQuery, esriSelectionResultEnum.esriSelectionResultNew, false);
            _MapControl.ActiveView.Refresh();
        }  

7、右键事件

右键事件里目前只实现了前四个:

首先是排序:

/// <summary>
        /// 数据排序
        /// </summary>
        /// <param name="pFeatureClass"></param>
        private void SortFeatures(IFeatureClass pFeatureClass)
        {
            ITableSort pTableSort = new TableSortClass();
            IFields pFields = pFeatureClass.Fields;
            IField pField = pFields.get_Field(col_index);

            pTableSort.Fields = pField.Name;

            if (up)
            {
                pTableSort.set_Ascending(pField.Name, true);
            }
            else
            {
                pTableSort.set_Ascending(pField.Name, false);
            }
            pTableSort.set_CaseSensitive(pField.Name, true);
            pTableSort.Table = pFeatureClass as ITable;
            pTableSort.Sort(null);
            ICursor pCursor = pTableSort.Rows;
            pTs = pTableSort;            
            RefreshTable();
        }


        /// <summary>
        /// 正序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolAsc_Click(object sender, EventArgs e)
        {
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            up = true;
            SortFeatures(pFLayer.FeatureClass);
        }

        /// <summary>
        /// 逆序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolDesc_Click(object sender, EventArgs e)
        {
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            up = false;
            SortFeatures(pFLayer.FeatureClass);
        }  

接下来就是删除字段:

/// <summary>
        /// 删除字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolDel_Click(object sender, EventArgs e)
        {
            int indexNum = gdvAttribute.CurrentCell.ColumnIndex;
            string strField = gdvAttribute.Columns[indexNum].HeaderText.ToString();
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            string strResult = "";
            if ((MessageBox.Show("确定要删除该字段吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes))
            {
                strResult = DeleteField(pFLayer, strField);
                gdvAttribute.Columns.Remove(strField);
                MessageBox.Show(strResult, "提示", MessageBoxButtons.OK);
            }               
        }

        /// <summary>
        /// 删除属性表字段
        /// </summary>
        /// <param name="layer">需要添加字段的IFeatureLayer</param>
        /// <param name="fieldName">添加的字段的名称</param>
        /// <returns></returns>
        public string DeleteField(IFeatureLayer layer, string fieldName)
        {
            try
            {
                ITable pTable = (ITable)layer;
                IFields pfields;
                IField pfield;
                pfields = pTable.Fields;
                int fieldIndex = pfields.FindField(fieldName);
                pfield = pfields.get_Field(fieldIndex);
                pTable.DeleteField(pfield);
                return "删除成功!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

最后一个功能就是属性表计算:

/// <summary>
        /// 字段计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolFieldCalculator_Click(object sender, EventArgs e)
        {
            int indexNum = gdvAttribute.CurrentCell.ColumnIndex;
            string strField = gdvAttribute.Columns[indexNum].HeaderText.ToString();
            ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
            IFeatureLayer pFLayer = pLayer as IFeatureLayer;
            formCalField formcalfield = new formCalField(pFLayer, gdvAttribute,strField);
            formcalfield.Show();
        }

属性表计算调用了窗口formCalField ,调用参数为IFeatureLayer pFeatureLayer,DataGridView dataGridView,string strField,那么看看formCalField 是如何设计的吧:

[attach]165443[/attach]

很简单,一个listbox,一个txtbox用与输入表达式,还有一个txtbox用于显示计算信息,其代码为:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geoprocessor;

namespace MapDemo
{
    public partial class formCalField : Form
    {
        private IFeatureLayer _FeatureLayer = null;
        private DataGridView Gridviwe;
        private string Field = "";
        public formCalField(IFeatureLayer pFeatureLayer,DataGridView dataGridView,string strField)
        {
            InitializeComponent();
            _FeatureLayer = pFeatureLayer;
            Field = strField;
            Gridviwe = dataGridView;
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formCalField_Load(object sender, EventArgs e)
        {
            IFeatureClass pFeatureCls = _FeatureLayer.FeatureClass;
            int FieldCount = pFeatureCls.Fields.FieldCount;
            for (int i = 0; i < FieldCount; i++)
            {
                IField pField = pFeatureCls.Fields.get_Field(i);
                //去除shape字段
                if (pField.Name == "shape")
                {
                    continue;
                }
                listField.Items.Add("["+pField.Name+"]");
            }
            lblField.Text = Field+"=";
        }

        /// <summary>
        /// 双击选择字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listField_DoubleClick(object sender, EventArgs e)
        {
            string strSelectField = this.listField.SelectedItem.ToString();
            txtExpression.Text = strSelectField;
        }

        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCal_Click(object sender, EventArgs e)
        {
            string strResult = FieldCal(_FeatureLayer, Field, txtExpression.Text);
            MessageBox.Show(strResult);
            this.Close();
            RefreshTable refreshtable = new RefreshTable();
            refreshtable.Refresh(Gridviwe, _FeatureLayer);
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void brnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string FieldCal(IFeatureLayer pFtLayer,string strField,string strExpression)
        {
            txtMessage.Text = "正在计算请稍后……\r\n";
            try
            {
                Geoprocessor Gp = new Geoprocessor();
                Gp.OverwriteOutput = true;
                CalculateField calField = new CalculateField();
                calField.in_table = pFtLayer as ITable;
                calField.field = strField;
                calField.expression = strExpression;

                Gp.Execute(calField, null);
                
                for (int i = 0; i < Gp.MessageCount; i++)
                {
                    txtMessage.Text += Gp.GetMessage(i).ToString() + "\r\n";
                }
                return "计算成功";
            }
            catch (Exception e)
            {
                txtMessage.Text += e.Message;
                return "计算失败"+e.Message;
            }            
        }
    }
}

字段计算我是通过调用Geopressor实现的。最后,粘出RefreshTable这个类的源代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;

namespace MapDemo
{
    public class RefreshTable
    {
        //刷新属性表
        public void Refresh(DataGridView dataGridView, IFeatureLayer pFeatureLayer)
        {
            IFeatureLayer pFLayer = pFeatureLayer;
            IFeatureClass pFeatureClass = pFLayer.FeatureClass;

            if (pFeatureClass == null) return;

            DataTable dt = new DataTable();
            DataColumn dc = null;

            for (int i = 0; i < pFeatureClass.Fields.FieldCount; i++)
            {

                dc = new DataColumn(pFeatureClass.Fields.get_Field(i).Name);

                dt.Columns.Add(dc);

            }

            IFeatureCursor pFeatureCuror = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCuror.NextFeature();

            DataRow dr = null;
            while (pFeature != null)
            {
                dr = dt.NewRow();
                for (int j = 0; j < pFeatureClass.Fields.FieldCount; j++)
                {
                    if (pFeatureClass.FindField(pFeatureClass.ShapeFieldName) == j)
                    {

                        dr[j] = pFeatureClass.ShapeType.ToString();
                    }
                    else
                    {
                        dr[j] = pFeature.get_Value(j).ToString();

                    }
                }

                dt.Rows.Add(dr);
                pFeature = pFeatureCuror.NextFeature();
            }
            dataGridView.DataSource = dt;
        }
    }
}

至此,属性表基本的操作基本上已经实现了,还望对大家有所帮助……同时,继续关注我的相关更新,你的关注是我发帖最大的动力!!!

AE属性表操作(1)

今天给大家分享一个AE属性表操作的程序源码,希望对有需要的筒子一定的帮助,小弟倍感荣幸! 好了,废话少说!首先说说实现了哪些操作吧…… 实现的操作包括:1、打开属性表;2、编辑属性表;3、增加属性...
  • lysc_forever
  • lysc_forever
  • 2013年12月03日 11:54
  • 7084

AE属性表操作

原文连接:http://blog.csdn.net/lysc_forever/article/details/17093405 实现的操作包括:1、打开属性表;2、编辑属性表;3、增加属性列;4...
  • chanyinhelv
  • chanyinhelv
  • 2015年02月07日 10:27
  • 6290

AE属性表操作

原文连接:http://blog.csdn.net/lysc_forever/article/details/17093405 实现的操作包括:1、打开属性表;2、编辑属性表;3、增加属性列;4、...
  • u013084746
  • u013084746
  • 2017年12月15日 16:01
  • 66

AE二次开发-获取图层的属性表

获取矢量或栅格图层的属性表,且可以指定获取的行数。 可以根据字段属性设置数据表的列属性。 /// /// 获取图层的属性表 /// ...
  • m0_37862405
  • m0_37862405
  • 2017年10月02日 20:31
  • 502

ArcGIS Engine10.0轻松入门级教程(3)——ArcEngine10.0查询分析功能

GIS中的查询分析功能是非常重要的,本节将实现这些功能。 1属性及空间查询        在Forms文件夹右击点击“添加”—>“Windows窗体”,添加两个窗体,分别用于空间查询和属性查询...
  • lianliancf
  • lianliancf
  • 2012年07月24日 22:52
  • 7691

AE属性查询(c#)

上面写了空间查询的小结,属性查询也少不了,已比较复杂的属性查询为示例总结下。  涉及的思路根据选择矢量数据的比例尺,列出一定比例尺下数据集中的图层列表(图层名、图层别名)--->根据选择的图...
  • u010499087
  • u010499087
  • 2014年03月14日 17:52
  • 2282

修改Feature的属性表的值

首先要获取Featureclass,利用Featureclass获取IWorkspace和IWorkspaceEdit:                 IWorkspace workspace...
  • anlidengshiwei
  • anlidengshiwei
  • 2014年11月14日 08:42
  • 1576

AE属性表操作(2)

转自:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=129398 6、选中行高亮在地图中显示 /// /// 该行...
  • lysc_forever
  • lysc_forever
  • 2013年12月03日 11:59
  • 2880

ALV中的fieldcat常用以及大全

字段目录是用来控制ALV显示的网格中每个字段的属性的,比如字段的顺序,对齐方式,可编辑状态,颜色,等等.常用的字段如下: row_pos:    默认值为0,可选值为1、2、3,既最...
  • zliguangyu
  • zliguangyu
  • 2013年03月06日 16:18
  • 9207

AE--属性表添加字段,修改字段属性

一、  //定义新字段             IField pField = new FieldClass();               //字段编辑            ...
  • qq_35040828
  • qq_35040828
  • 2017年06月15日 21:26
  • 527
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AE属性表操作(2)
举报原因:
原因补充:

(最多只允许输入30个字)