In this example we will suppose we have a windows application that contains a map control called 'ctMap', a list box called 'lstLayers' and another windows form that contains a DataGrid with public access modifier called dg, we will use this data grid to show the generated data table.
This function will load an Mxd file to the map control:
private void loadMXD(string mxdPath)
{
IMapDocument doc = new MapDocumentClass();
doc.Open(mxdPath, "");
IMap map = doc.get_Map(0);
ctMap.Map = map;
// we can't use foreach directlty with map
MapExplorer mex = new MapExplorer(map);
lstLayers.Items.Clear();
foreach (ILayer lyr in mex.Layers)
{
lstLayers.Items.Add(lyr.Name);
}
}
This function will generate the data table from the layer and shows it in a form:
private void openAttributeTable(int layerIndex)
{
ILayer lyr = ctMap.get_Layer(layerIndex);
IFeatureLayer flyr = lyr as IFeatureLayer;
if (flyr != null)
{
// create a new DataTableBuilder instance
DataTableBuilder tableBuilder = new DataTableBuilder(flyr.FeatureClass);
// get the generated datatable
DataTable dtable = tableBuilder.DataTable;
// create a feature cursor with recycling enabled
IFeatureCursor crsr = flyr.Search(null, true);
// create a new DataTableAdapter Instance
DataTableAdapter tableAdapter = new DataTableAdapter(crsr);
// now fill the datatable
tableAdapter.Fill(dtable);
FrmAttribTable f = new FrmAttribTable();
f.dg.DataSource = dtable;
f.Show();
}
}
Happy Coding...
EAGL Project Site
No comments:
Post a Comment