以下代码实现了,根据给定的索引值layerindex,图层名称显示在Label中,将图层的属性名称添加到CheckedListBox中,将属性展示在DataGridView中。
ILayer layer = mainForm.axMapControltemp.get_Layer(layerindex);
IFeatureLayer pFeatureLayer = layer as IFeatureLayer;
label1.Text = "您选择的是:"+pFeatureLayer.Name+" 图层";
//加载属性数据
ITable table = pFeatureLayer.FeatureClass as ITable;
dataGridView1.ColumnCount = table.Fields.FieldCount;//要先指定<span style="font-family: Arial, Helvetica, sans-serif;">dataGridView1的列数</span>
for (int i = 0; i < table.Fields.FieldCount; i++)
{
checkedListBox1.Items.Add(table.Fields.get_Field(i).Name.ToString());
dataGridView1.Columns[i].Name = table.Fields.get_Field(i).Name.ToString();//属性名赋给列名
dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//自适应列宽
}
for (int i = 0; i < table.RowCount(null); i++)
{
int index = dataGridView1.Rows.Add();//获取当前行
for (int j = 0; j < table.Fields.FieldCount; j++)
{
if (table.Fields.get_Field(j).Type == esriFieldType.esriFieldTypeGeometry)//Arcgis属性中的特殊属性类型处理
dataGridView1.Rows[index].Cells[j].Value = GetFeatureShapeType(pFeatureLayer);
else
dataGridView1.Rows[index].Cells[j].Value = table.GetRow(i).get_Value(j).ToString();
}
}
private String GetFeatureShapeType(IFeatureLayer pFeatureLayer)//获取图层Shape类型的方法
{
String str;
str = "error";
if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
str = "Point";
}
else if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
str = "Line";
}
else if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
str = "Polygon";
}
return str;
}