书上的例子不大好,还好网上资料挺多。
就参考网上各种资料的学习过程:
https://blog.csdn.net/lysc_forever/article/details/17093405
这篇写得很详细了,不过,根据我自己的情况,有些方面修改了一下。
---------------------------------------------------------------------------------------------------------------------
一、打开属性表
1、将contextMenuStrip拖到控件上,再添加一个“打开属性表”。这就是TOCCControl的右键菜单了
2、新建一个窗体文件AttributeTable,将menuStrip和dataGridView拖到窗体上。menuStrip上还会加一些功能,之后再说,现在先不管。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
namespace demo1地图浏览
{
public partial class AttributeTable : Form
{
public DataTable attributeTable;
ILayer mLayer;
public AttributeTable(ILayer layer)
{
InitializeComponent();
mLayer = layer;
}
private void AttributeTable_Load(object sender, EventArgs e)
{
IFeatureLayer pFeatureLayer = mLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
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();
}
dataGridView1.DataSource = dt;
}
}
}
}
3、回到主窗体,添加TOCCControl的OnMouseDown事件,其实就是右键
public ILayer layer; //这个是全局变量
private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
{
if (e.button == 2)
{
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
IBasicMap map = new MapClass();
layer = new FeatureLayerClass();
object other = new object();
object index = new object();
axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index); //实现赋值,ref的参数必须初始化
if (item == esriTOCControlItem.esriTOCControlItemLayer) 点击的是图层的话,就显示右键菜单
{
contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
}
}
}
再在“打开属性表”上点两下,其实是添加它的Click事件
private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
AttributeTable attributeTable = new AttributeTable(layer);
attributeTable.Text = "属性表:" + layer.Name;
attributeTable.ShowDialog();
}
打开属性表的功能就完成了,看看效果
二、点击属性表上的数据,图层上高亮显示
上一步骤里面主窗体里的有一段代码是这样的
private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
AttributeTable attributeTable = new AttributeTable(layer);
attributeTable.Text = "属性表:" + layer.Name;
attributeTable.ShowDialog();
}
改成这样:其实就是换成this了,把主窗体传进子窗体,便于在子窗体调用主窗体的控件。把axMapControl控件的Modifier属性改为public。(其实这样用不大好,最好是用委托,但先这样吧,委托等我以后再说)
private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
AttributeTable attributeTable = new AttributeTable(this);
attributeTable.Text = "属性表:" + layer.Name;
attributeTable.ShowDialog();
}
然后到AttributeTable里去,加个全局变量 private Form1 form。把构造函数改成这样:
private Form1 form1;
public AttributeTable(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
mLayer = form1.layer;
}
添加dataGridView的RowHeaderMouseClick事件,选中高亮显示的代码如下:
//选中高亮显示
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
IQueryFilter pQuery = new QueryFilterClass();
int count = this.dataGridView1.SelectedRows.Count;
string val, col;
col = this.dataGridView1.Columns[0].Name;
//当只选中一行时
if (count == 1)
{
val = this.dataGridView1.SelectedRows[0].Cells[col].Value.ToString();
//设置高亮要素的查询条件
pQuery.WhereClause = col + "=" + val;
}
else//选中多行
{
int i;
string str;
for (i = 0; i < count - 1; i++)
{
val = this.dataGridView1.SelectedRows[i].Cells[col].Value.ToString();
str = col + "=" + val + "OR";
pQuery.WhereClause += str;
}
//添加最后一个要素的条件
val = this.dataGridView1.SelectedRows[i].Cells[col].Value.ToString();
str = col + "=" + val;
pQuery.WhereClause += str;
}
//ILayer pLayer = (ILayer)m_mapControl.CustomProperty;
IFeatureLayer pFLayer = mLayer as IFeatureLayer;
IFeatureSelection pFeatSelection;
pFeatSelection = pFLayer as IFeatureSelection;
pFeatSelection.SelectFeatures(pQuery, esriSelectionResultEnum.esriSelectionResultNew, false);
IViewRefresh viewRefresh = form1.axMapControl1.Map as IViewRefresh;
viewRefresh.ProgressiveDrawing = true;
viewRefresh.RefreshItem(mLayer);
}
效果就是这样的:
这一篇先写这两个操作,还有一些操作就写到下一篇里了