浏览属性表

 栅格图层和矢量图层的属性表浏览
if (pLyr is IFeatureLayer)
            {
                DataTable pTable = new DataTable();
                IFeatureLayer pFealyr = pLyr as IFeatureLayer;
                IFeatureClass pFCls = pFealyr.FeatureClass;
                string shape = "";
                if (pFCls.ShapeType == esriGeometryType.esriGeometryPoint)
                    shape = "Point";
                else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolyline)
                    shape = "Polyline";
                else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolygon)
                    shape = "Polygon";

                for (int i = 0; i < pFCls.Fields.FieldCount; i++)
                {
                    pTable.Columns.Add(pFCls.Fields.get_Field(i).Name);
                }
                IFeatureCursor pCursor = pFCls.Search(null, false);
                int ishape = pFCls.Fields.FindField("Shape");
                IFeature pFea = pCursor.NextFeature();
                while (pFea != null)
                {
                    DataRow pRow = pTable.NewRow();
                    for (int i = 0; i < pFCls.Fields.FieldCount; i++)
                    {
                        if (i == ishape)
                        {
                            pRow[i] = shape;
                            continue;
                        }
                        pRow[i] = pFea.get_Value(i).ToString();
                    }
                    pTable.Rows.Add(pRow);
                    pFea = pCursor.NextFeature();
                }
                dataGridView1.DataSource = pTable;
            }
            else if (pLyr is IRasterLayer)
            {
                IRasterLayer pRlyr = pLyr as IRasterLayer;
                IRaster pRaster = pRlyr.Raster;
                IRasterProps pProp = pRaster as IRasterProps;
                pProp.PixelType = rstPixelType.PT_LONG;
                if (pProp.PixelType == rstPixelType.PT_LONG)
                {
                    IRasterBandCollection pBcol = pRaster as IRasterBandCollection;
                    IRasterBand pBand =  pBcol.Item(0);
                    ITable pRTable =  pBand.AttributeTable;              

                    DataTable pTable = new DataTable();
                    for (int i = 0; i < pRTable.Fields.FieldCount; i++)
                        pTable.Columns.Add(pRTable.Fields.get_Field(i).Name);

                  
                     ICursor pCursor=    pRTable.Search(null, false);
                     IRow pRrow=   pCursor.NextRow();
                     while (pRrow != null)
                     {
                         DataRow pRow = pTable.NewRow();
                         for (int i =0 ;i<pRrow .Fields .FieldCount ;i++)
                         {
                             pRow[i] =  pRrow.get_Value(i).ToString ()   ;
                         }
                         pTable.Rows.Add(pRow);
                         pRrow = pCursor.NextRow();
                     }
                     dataGridView1.DataSource = pTable;
                }              

            }

你可能感兴趣的:(属性)