TOCControl控件使用的是用伙伴控件中的数据地图,它控制图层是否在伙伴控件空显示以及和伙伴控件在符号上保持一致,TOCControl为用户提供了一个交互式的环境,如果TOCControl控件的伙伴控件是MapControl控件,当我们将TOCControl控件中图层删掉是,MapControl控件中相应的图层也会被删掉。
显示属性表的信息
而ArcGIS Engine提供的TOCControl控件几乎没有提供,那么这些都是需要自己开发的,在这里我做一个显示属性表的功能。
分析:要显示某一个图层的属性表,首先要将这个图层选中,然后在另外一个Form中将选中的这个图层的属性信息进行显示。
添加一个上下文菜单,添加一个新的Form窗体,在这个新的窗体上添加GridView控件,并在TOCControl控件的OnMouseDown事件下添加如下代码(pGlobalFeatureLayer是我定义的一个全局变量):
private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
{
if (axMapControl1.LayerCount > 0)
{
esriTOCControlItem pItem = new esriTOCControlItem();
pGlobalFeatureLayer = new FeatureLayerClass();
IBasicMap pBasicMap = new MapClass();
object pOther = new object();
object pIndex = new object();
axTOCControl1.HitTest(e.x, e.y, ref pItem, ref pBasicMap, ref pGlobalFeatureLayer, ref pOther, ref pIndex);
}
if (e.button == 2)
{
context.Show(axTOCControl1, e.x, e.y);
}
}
在上下文菜单的打开属性表的Click事件中添加如下代码:
private void 打开属性表ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormTable Ft = new FormTable(pGlobalFeatureLayer as IFeatureLayer);
Ft.Show();
}
在新的窗体中添加一个将属性表显示到GridView控件中的函数,如下:
public void Itable2Dtable()
{
IFields pFields;
pFields = pFeatureLayer.FeatureClass.Fields;
dtGridView.ColumnCount = pFields.FieldCount;
for (int i = 0; i < pFields.FieldCount;i++ )
{
string fldName = pFields.get_Field(i).Name;
dtGridView.Columns[i].Name = fldName;
dtGridView.Columns[i].ValueType = System.Type.GetType(ParseFieldType(pFields.get_Field(i).Type));
}
IFeatureCursor pFeatureCursor;
pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
IFeature pFeature;
pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
string[] fldValue = new string[pFields.FieldCount];
for (int i = 0; i < pFields.FieldCount; i++)
{
string fldName;
fldName = pFields.get_Field(i).Name;
if (fldName==pFeatureLayer .FeatureClass .ShapeFieldName)
{
fldValue[i] = Convert.ToString(pFeature.Shape.GeometryType);
}
else
fldValue[i] = Convert.ToString(pFeature.get_Value(i));
}
dtGridView.Rows.Add(fldValue);
pFeature = pFeatureCursor.NextFeature();
}
}
参考:http://blog.sina.com.cn/s/blog_84f7fbbb01013ozt.html
http://blog.sina.com.cn/s/blog_8f64865301017rmb.html
另datagridview随form窗体大小变化:http://blog.csdn.net/fengxing11/article/details/52527715