精卫鸟的天空

思想有多远,我就能飞多远...... (致力于GIS方面的开发,希望每天能进步一点!)

 

打开要素层的属性表


      前不久在网站上看了其它同行用VB写的关于打开要素层属性表的代码,我把它改了一下用C#写了出来,希望对大家的学习用能有所帮助!

private void OpenAttribute_Load(object sender, System.EventArgs e)
        
{
            
//得到要素层的接口
            IFeatureLayer pFeatLyr=(IFeatureLayer)axMapControl1.get_Layer(0);
            
//查询ILayerFields接口
            ILayerFields pFeatLyrFields=(ILayerFields)pFeatLyr;
            IFeatureClass pFeatCls
=pFeatLyr.FeatureClass;
            
            DataSet myDataset
=new DataSet("Test");
            DataTable pTable
=new DataTable(pFeatLyr.Name);
            
for(int n=0;n<pFeatLyrFields.FieldCount-1;n++)
            
{
                DataColumn pTableCol
=new DataColumn(pFeatLyrFields.get_Field(n).Name);
                pTable.Columns.Add(pTableCol);
                pTableCol
=null;
            }

            IFeatureCursor pFeatCursor
=pFeatCls.Search(null,false);
            IFeature pFeat
=pFeatCursor.NextFeature();
            
while (pFeat!=null)
            
{
                DataRow pTableRow
=pTable.NewRow();
                
for(int i=0;i<pFeatLyrFields.FieldCount-1;i++)
                
{
                    
if(pFeatLyrFields.FindField(pFeatCls.ShapeFieldName)==i)
                    
{
                        pTableRow[i]
=pFeatCls.ShapeType.ToString();
                    }

                    
else
                    
{
                        pTableRow[i]
=pFeat.get_Value(i).ToString();
                    }

                }

                pTable.Rows.Add(pTableRow);
                pFeat
=pFeatCursor.NextFeature();
            }

            myDataset.Tables.Add(pTable);
            dataGrid1.DataSource
=myDataset;
            dataGrid1.DataMember
=pFeatLyr.Name;
        }


        
//选择和编辑属性表
        private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        
{
            DataGrid myGrid
=(DataGrid)sender;
            DataGrid.HitTestInfo myHitTestInfo
=myGrid.HitTest(e.X,e.Y);
            
string message = "You clicked ";

            
switch (myHitTestInfo.Type) 
            
{
                
case System.Windows.Forms.DataGrid.HitTestType.None :
                    message 
+= "the background.";
                    
break;
                
case System.Windows.Forms.DataGrid.HitTestType.Cell :
                    message 
+= "cell at row " + myHitTestInfo.Row + ", col " + myHitTestInfo.Column;
                    
break;
                
case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader :
                    message 
+= "the column header for column " + myHitTestInfo.Column;
                    
break;
                
case System.Windows.Forms.DataGrid.HitTestType.RowHeader :
                    message 
+= "the row header for row " + myHitTestInfo.Row;
                    
break;
                
case System.Windows.Forms.DataGrid.HitTestType.ColumnResize :
                    message 
+= "the column resizer for column " + myHitTestInfo.Column;
                    
break;
                
case System.Windows.Forms.DataGrid.HitTestType.RowResize :
                    message 
+= "the row resizer for row " + myHitTestInfo.Row;
                    
break;
                
case System.Windows.Forms.DataGrid.HitTestType.Caption :
                    message 
+= "the caption";
                    
break;
                
case System.Windows.Forms.DataGrid.HitTestType.ParentRows :
                    message 
+= "the parent row";
                    
break;
            }


            Console.WriteLine(message);

        }

posted on 2006-11-24 23:22  vakinglee  阅读(1056)  评论(1编辑  收藏  举报

导航