转自: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;
}
}
}
至此,属性表基本的操作基本上已经实现了,还望对大家有所帮助……同时,继续关注我的相关更新,你的关注是我发帖最大的动力!!!