I recently read other code written by my peers using VB on the website about opening the element layer Attribute Table. I changed it and wrote it in C, I hope it will be helpful for everyone to learn and use it!
Private void OpenAttribute_Load (object sender, System. EventArgs e)
{
// Obtain the element layer interface
IFeatureLayer pFeatLyr = (IFeatureLayer) axMapControl1.get _ Layer (0 );
// Query the ILayerFields Interface
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;
}
// Select and edit the Attribute Table
Private void datagrid=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 );
}