http://www.gissky.net- GIS空间站

我要投稿 投稿指南 RSS订阅 网站资讯通告:
搜索: 您现在的位置: GIS空间站 >> 技术专栏 >> ArcGIS >> ArcObjects开发 >> 正文

《ArcGIS Engine+C#实例开发教程》第八讲 属性数据表的查询显示

作者:3SDN.Net    文章来源:3SDN原创    点击数:    更新时间:2009-5-13
摘要:这一讲中,我们将实现图层属性数据表的查询显示。在ArcMap中,单击图层右键菜单中的“Open Attribute Table”命令,便可弹出属性数据表。本讲将完成类似的功能.

3.装载DataTable数据

从上一步得到的DataTable还没有数据,只有字段信息。因此,我们要通过ICursor从ITable中逐一取出每一行数据,即IRow。再创建DataTable中相应的DataRow,根据IRow设置DataRow信息,再将所有的DataRow添加到DataTable中,就完成了DataTable数据的装载。

为保证效率,一次最多只装载2000条数据到DataGridView。函数代码如下:

 /// <summary>

/// 填充DataTable中的数据

/// </summary>

/// <param name="pLayer"></param>

/// <param name="tableName"></param>

/// <returns></returns>

public static DataTable CreateDataTable(ILayer pLayer, string tableName)

{

//创建空DataTable

DataTable pDataTable = CreateDataTableByLayer(pLayer, tableName);

//取得图层类型

string shapeType = getShapeType(pLayer);

//创建DataTable的行对象

DataRow pDataRow = null;

//从ILayer查询到ITable

ITable pTable = pLayer as ITable;

ICursor pCursor = pTable.Search(null, false);

//取得ITable中的行信息

IRow pRow = pCursor.NextRow();

int n = 0;

while (pRow != null)

{

//新建DataTable的行对象

pDataRow = pDataTable.NewRow();

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

{

//如果字段类型为esriFieldTypeGeometry,则根据图层类型设置字段值

if (pRow.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)

{

pDataRow[i] = shapeType;

}

//当图层类型为Anotation时,要素类中会有esriFieldTypeBlob类型的数据,

//其存储的是标注内容,如此情况需将对应的字段值设置为Element

else if (pRow.Fields.get_Field(i).Type == esriFieldType.esriFieldTypeBlob)

{

pDataRow[i] = "Element";

}

else

{

pDataRow[i] = pRow.get_Value(i);

}

}

//添加DataRow到DataTable

pDataTable.Rows.Add(pDataRow);

pDataRow = null;

n++;

//为保证效率,一次只装载最多条记录

if (n == 2000)

{

pRow = null;

}

else

{

pRow = pCursor.NextRow();

}

}

return pDataTable;

}

上面的代码中涉及到一个获取图层类型的函数getShapeTape,此函数是通过ILayer判断图层类型的,代码如下:

/// <summary>

/// 获得图层的Shape类型

/// </summary>

/// <param name="pLayer">图层</param>

/// <returns></returns>

public static string getShapeType(ILayer pLayer)

{

IFeatureLayer pFeatLyr = (IFeatureLayer)pLayer;

switch (pFeatLyr.FeatureClass.ShapeType)

{

case esriGeometryType.esriGeometryPoint:

return "Point";

case esriGeometryType.esriGeometryPolyline:

return "Polyline";

case esriGeometryType.esriGeometryPolygon:

return "Polygon";

default:

return "";

}

}

4.绑定DataTable到DataGridView

通过以上步骤,我们已经得到了一个含有图层属性数据的DataTable。现定义一个AttributeTableFrm类的成员变量:

 public DataTable attributeTable;

通过以下函数,我们很容易将其绑定到DataGridView控件中。

 /// <summary>

/// 绑定DataTable到DataGridView

/// </summary>

/// <param name="player"></param>

public void CreateAttributeTable(ILayer player)

{

string tableName;

tableName = getValidFeatureClassName(player .Name );

attributeTable = CreateDataTable(player,tableName );

this.dataGridView1 .DataSource = attributeTable ;

this.Text = "属性表[" + tableName + "] " + "记录数:"+attributeTable.Rows.Count .ToString();

}

因为DataTable的表名不允许含有“.”,因此我们用“_”替换。函数如下:

/// <summary>

/// 替换数据表名中的点

/// </summary>

/// <param name="FCname"></param>

/// <returns></returns>

public static string getValidFeatureClassName(string FCname)

{

int dot = FCname.IndexOf(".");

if (dot != -1)

{

return FCname.Replace(".", "_");

}

return FCname;

}

上一页  [1] [2] [3] 下一页

Tags:ArcGIS Engine  
责任编辑:gissky
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 中国地图