显示属性表

ArcGisEngine实现显示属性表:

 效果:


 
 

 一、拖入DataGridView

   从工具箱拖一个DataGridView控件到窗体.

 

 二、创建空的DataTable

   首先传入ILayer,在查询ITable,在从ITable中的Filed中的Fileds中获取每个Field,在根据Field设置DataTable的DataColumn,由此创建一个空的DataTable

 private static DataTable CreateDataTableByLayer(ILayer pLayer, string tableName)
{
        //创建一个DataTable表
        DataTable pDataTable = new DataTable(tableName);
        //取得ITable接口
        ITable pTable = pLayer as ITable;
        IField pField = null;
        DataColumn pDataColumn;
        //根据每个字段的属性建立DataColumn对象
        for (int i = 0; i < pTable.Fields.FieldCount; i++)
       {
       pField = pTable.Fields.get_Field(i);
       //新建一个DataColumn并设置其属性
       pDataColumn = new DataColumn(pField.Name);
       if (pField.Name == pTable.OIDFieldName)
       {
       pDataColumn.Unique = true;//字段值是否唯一
        }
       //字段值是否允许为空
       pDataColumn.AllowDBNull = pField.IsNullable;
       //字段别名
       pDataColumn.Caption = pField.AliasName;
      //字段数据类型
      pDataColumn.DataType = System.Type.GetType(ParseFieldType(pField.Type));
      //字段默认值
      pDataColumn.DefaultValue = pField.DefaultValue;
      //当字段为String类型是设置字段长度
      if (pField.VarType == 8)
      {
     pDataColumn.MaxLength = pField.Length;
      }
     //字段添加到表中
     pDataTable.Columns.Add(pDataColumn);
     pField = null;
     pDataColumn = null;
     }
     return pDataTable;
}

 //因为GeoDatabase的数据类型与.NET的数据类型不同,故要进行转换。转换函数如下:
public static string ParseFieldType(esriFieldType fieldType)
{
    switch (fieldType)
    {
    case esriFieldType.esriFieldTypeBlob:
    return "System.String";
    case esriFieldType.esriFieldTypeDate:
    return "System.DateTime";
    case esriFieldType.esriFieldTypeDouble:
    return "System.Double";
    case esriFieldType.esriFieldTypeGeometry:
    return "System.String";
    case esriFieldType.esriFieldTypeGlobalID:
    return "System.String";
    case esriFieldType.esriFieldTypeGUID:
    return "System.String";
    case esriFieldType.esriFieldTypeInteger:
    return "System.Int32";
    case esriFieldType.esriFieldTypeOID:
    return "System.String";
    case esriFieldType.esriFieldTypeRaster:
    return "System.String";
    case esriFieldType.esriFieldTypeSingle:
    return "System.Single";
    case esriFieldType.esriFieldTypeSmallInteger:
    return "System.Int32";
    case esriFieldType.esriFieldTypeString:
    return "System.String";
    default:
    return "System.String";
   }
}

 

  三、装载数据:

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;
        }

        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 "";
            }
}

 

 

四、绑定DataTable到DataGridView

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();
        }

        public static string getValidFeatureClassName(string FCname)
        {
            int dot = FCname.IndexOf(".");
            if (dot != -1)
            {
                return FCname.Replace(".", "_");
            }
            return FCname;
}

 

 

 五、调用并显示:

   1.在axTOCControl1_OnMouseDown方法中得到ILayer,赋countTableLayer.

 

   if (e.button == 2)
  {
                axMapControl1.CustomProperty = pLyr;
                //将当前图层传给countTableLayer.便于在功能5中使用
                countTableLayer = pLyr;
                if (pLyr != null && pItem == esriTOCControlItem.esriTOCControlItemLayer)
                    contextMenuStrip1.Show(axTOCControl1, e.x, e.y);
   }

 

 2.在显示属性表ToolStripMenuItem_Click()中调用.

 private void 显示属性表ToolStripMenuItem_Click(object sender, EventArgs e)
  {

            CreateAttributeTable(countTableLayer);
  }

 

   六、更多及详情:

http://www.gissky.net/Article/1574.htm

你可能感兴趣的:(ArcgisEngine,显示数据表)