frmTable.cs
上传用户:xianghe012
上传日期:2022-07-02
资源大小:77k
文件大小:4k
源码类别:

GIS编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ESRI.ArcGIS.esriSystem;
  9. using ESRI.ArcGIS.SystemUI;
  10. using ESRI.ArcGIS.Geometry;
  11. using ESRI.ArcGIS.Display;
  12. using ESRI.ArcGIS.Geodatabase;
  13. using ESRI.ArcGIS.Carto;
  14. using ESRI.ArcGIS.Controls;
  15. namespace ArcMap
  16. {
  17.     public partial class frmTable : Form
  18.     {
  19.         public IMapControl2 pMapControl;
  20.         public IMap pMap;
  21.         public int iLayerIndex;
  22.         public string LayerName;
  23.         public frmTable(IMapControl2 pFMapControl,string LyrName)
  24.         {
  25.             InitializeComponent();
  26.             pMapControl=pFMapControl;
  27.             pMap = pMapControl.Map;
  28.             LayerName = LyrName;
  29.         }
  30.         private void frmTable_Load(object sender, EventArgs e)
  31.         {
  32.             GetAllValues();
  33.         }
  34.         //获取属性表的内容
  35.         public void GetAllValues()
  36.         {
  37.             IFeatureLayer pFeatureLayer;
  38.             for (int i = 0; i <pMap.LayerCount;i++ )
  39.             {
  40.                 if (LayerName==pMap.get_Layer(i).Name)
  41.                 {
  42.                     iLayerIndex = i;
  43.                     break;
  44.                 }
  45.             }
  46.             pFeatureLayer = (IFeatureLayer)pMap.get_Layer(iLayerIndex);
  47.             IFields pFields;
  48.             pFields = pFeatureLayer.FeatureClass.Fields;
  49.             dtGridView.ColumnCount = pFields.FieldCount;
  50.             for (int i = 0; i < pFields.FieldCount;i++ )
  51.             {
  52.                 string fldName;
  53.                 fldName = pFields.get_Field(i).Name;
  54.                 dtGridView.Columns[i].Name = fldName;               
  55.                 
  56.             }           
  57.             IFeatureCursor pFeatureCursor;
  58.             pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
  59.             long lTotalRecords=1;
  60.             IFeature pFeature;
  61.             pFeature = pFeatureCursor.NextFeature();
  62.             while (pFeature != null)
  63.             {
  64.                 string[] fldValue = new string[pFields.FieldCount]; 
  65.                 for (int i = 0; i < pFields.FieldCount; i++)
  66.                 {
  67.                     string fldName;
  68.                     fldName = pFields.get_Field(i).Name;
  69.                     if (fldName=="Shape")
  70.                     {
  71.                         fldValue[i] = Convert.ToString(pFeature.Shape.GeometryType);
  72.                     }
  73.                     else
  74.                         fldValue[i] = Convert.ToString(pFeature.get_Value(i));
  75.                 }                
  76.                 dtGridView.Rows.Add(fldValue);
  77.                 pFeature = pFeatureCursor.NextFeature();
  78.                 lTotalRecords++;
  79.             }
  80.             tbarTotalRecords.Text = "共有" + Convert.ToString(lTotalRecords - 1) + "条记录";
  81.         }
  82.         private void dtGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  83.         {
  84.            string FID;
  85.            FID = dtGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
  86.            if (FID == "")
  87.                return;
  88.            IActiveView pActiveView;
  89.            pActiveView = (IActiveView)pMap;
  90.            pMap.ClearSelection();
  91.            pActiveView.Refresh();
  92.            IQueryFilter pQueryFilter = new QueryFilterClass();
  93.            pQueryFilter.WhereClause = "FID=" + FID;
  94.             IFeatureLayer pFeatureLayer;
  95.             pFeatureLayer =(IFeatureLayer)pMap.get_Layer(iLayerIndex);
  96.             IFeatureCursor pFeatureCursor;
  97.             pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false);
  98.             IFeature pFeature;
  99.             pFeature = pFeatureCursor.NextFeature();
  100.             pMap.SelectFeature(pFeatureLayer, pFeature);
  101.             IPoint pPoint = new PointClass();
  102.             pPoint.X = (pFeature.Extent.XMin + pFeature.Extent.XMax) / 2;
  103.             pPoint.Y = (pFeature.Extent.YMin + pFeature.Extent.YMax) / 2;
  104.             
  105.             pMapControl.CenterAt(pPoint);
  106.             pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
  107.         }
  108.     }
  109. }