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属性表操作

原文连接:http://blog.csdn.net/lysc_forever/article/details/17093405 实现的操作包括:1、打开属性表;2、编辑属性表;3、增加属性列;4...

AE属性表操作(1)

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

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

获取矢量或栅格图层的属性表,且可以指定获取的行数。 可以根据字段属性设置数据表的列属性。 /// /// 获取图层的属性表 /// ...

AE属性表操作(1)

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

C#用QueryTable向excel批量导入数据

必须引用com组件参考代码:using System; using System.Collections.Generic; using System.Text; using System.Reflec...
  • rose001
  • rose001
  • 2009年05月31日 16:48
  • 1033

(续)AE中读取所打开shp的属性表

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

显示栅格图层和矢量图层的属性表(AE开发)

转载自:http://blog.csdn.net/xwebsite/article/details/4850383显示栅格图层和矢量图层的属性表定义一个函数showAttributeTable()用于...

AE实现右键点击TOC控件任意图层显示属性表

private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouse...

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

一、  //定义新字段             IField pField = new FieldClass();               //字段编辑            ...

QT属性表改变值操作demo

  • 2017年03月16日 10:48
  • 4.86MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AE属性表操作(2)
举报原因:
原因补充:

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