C#+AE:如何用TreeList控件实现属性查询

闲话不多,代码如下:

     private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.mxd|*.mxd";
            ofd.ShowDialog();

            string fp = ofd.FileName;
            axMapControl1.LoadMxFile(fp,0,Type.Missing);
        }

        bool bu = false;
        private void button2_Click(object sender, EventArgs e)
        {
            bu = true;

        }

 

     private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {

            try
            {
                if (bu)
                {
                    //先创建一张表,按照自己的想法;需要两张表???
                    //或者创建List也行,需要合适的方法赋值给TreeList
                    DataTable pDataTable = new DataTable();
                    pDataTable.Columns.Add("ID");
                    pDataTable.Columns.Add("Name");
                    pDataTable.Columns.Add("ParentID");

                    DataRow pDataRow = null;
                    for (int i = 0; i < axMapControl1.Map.LayerCount;i++ )
                    {
                        pDataRow=pDataTable.NewRow();
                        string lyrName = axMapControl1.Map.get_Layer(i).Name;
                        pDataRow["ID"] = lyrName;
                        pDataRow["Name"] = lyrName;
                        pDataRow["ParentID"] = -1;
                        pDataTable.Rows.Add(pDataRow);

 

                       
                        //开始点选查询
                        IMap pMap;
                        pMap = axMapControl1.Map as IMap;

                        //获取点图层
                        IFeatureLayer pFeatureLayer;
                        pFeatureLayer = pMap.get_Layer(i) as IFeatureLayer;
                        IFeatureClass pFeatureClass;
                        pFeatureClass = pFeatureLayer.FeatureClass;

                        //获取鼠标点击点
                        IPoint pPoint;
                        pPoint = new PointClass();
                        pPoint.PutCoords(e.mapX, e.mapY);

                        IGeometry pGeometry;

                        //定义缓冲区
                        double db = 2;
                        ITopologicalOperator pTop;
                        pTop = pPoint as ITopologicalOperator;
                        pGeometry = pTop.Buffer(db);

                        //选取
                        pMap.SelectByShape(pGeometry, null, false);
                        pMap.ClearSelection();

                        //空间过滤运算
                        ISpatialFilter pSpatialFilter = new SpatialFilterClass();
                        pSpatialFilter.Geometry = pGeometry;


                        //设置为不同的要素类型的图层
                        switch (pFeatureClass.ShapeType)
                        {
                            case esriGeometryType.esriGeometryPoint:
                                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                                break;
                            case esriGeometryType.esriGeometryPolyline:
                                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
                                break;
                            case esriGeometryType.esriGeometryPolygon:
                                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                                break;

                        }
                        pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;

                        //指针
                        IFeatureCursor pFeatureCursor;
                        pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
                        IFeature pFeature;
                        pFeature = pFeatureCursor.NextFeature();

                        //开始遍历
                        while (pFeature != null)
                        {

                            //获取要素的字段名和字段值
                            int n = pFeature.Fields.FieldCount; //这句话很重要,得到字段数目;
                            string sName;
                           // object oValue;
                            string sValue;
                          

                            //这句话的对象需要随着地图的改变而改变。可以是ID,FID 等带有唯一标识身份的 东西。
                                int index=pFeature.Fields.FindField("ObjectID");
                                if (index == -1)
                                    return;
                                IField pField = pFeature.Fields.get_Field(index);
                                sName = pField.Name;
                              
                                sValue = pFeature.get_Value(index).ToString();
                               
                              
                                    pDataRow = pDataTable.NewRow();
                                   
                            //之所以这样赋值是为了保证ID的唯一性;
                                    pDataRow["ID"] = lyrName + sValue;
                                    //赋值给ObjectID等的值,这样是为了好合并表;
                                    pDataRow["Name"] = sValue;
                                    pDataRow["ParentID"] = lyrName;
                                    pDataTable.Rows.Add(pDataRow);


                                //}

                              

                            //}
                            pFeature = pFeatureCursor.NextFeature();

 

                        }
                    }
                   //   dataGridView1.DataSource = pDataTable;
                    treeList1.DataSource = pDataTable;
                    treeList1.ParentFieldName="ParentID";
                

                 
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

你可能感兴趣的:(list)