1. 程式人生 > >TOCControl 打開圖層屬性表(轉)

TOCControl 打開圖層屬性表(轉)

cat 事件 layer window data component 右鍵 relay catch

首先添加一個新的windows窗體frmAttribute,在上面添加個datagridview控件,用來顯示圖層屬性!

定義frmAttribute中的全局變量:
 private ILayer pLayer;;//打開屬性表的圖層
 private IFeatureLayer pFeatureLayer;
 private IFeatureClass pFeatureClass;
private ILayerFields pLayerFields;

 修改構造函數,傳入要打開屬性表的圖層:
public frmAttribute( ILayer pLyr)
{
 InitializeComponent();
// m_MapControl = pMapControl; pLayer = pLyr; }

 在右鍵菜單的打開屬性表選項單擊事件總打開frmAttribute窗口:
private void AttributesToolStripMenuItem_Click(object sender, EventArgs e)
 {
 frmAttribute newFA = new frmAttribute( p_Layer);//傳入圖層,在右擊事件裏返回的圖層
 newFA.Show();
 }
 窗機加載時讀取信息:
private void frmAttribute_Load(object
sender, EventArgs e)
{
try { pFeatureLayer = pLayer as IFeatureLayer; pFeatureClass = pFeatureLayer.FeatureClass; pLayerFields = pFeatureLayer as ILayerFields; DataSet ds = new DataSet("dsTest"); DataTable dt = new DataTable(pFeatureLayer.Name); DataColumn dc = null; for (int i = 0; i < pLayerFields.FieldCount; i++) { dc
= new DataColumn(pLayerFields.get_Field(i).Name); //if (pLayerFields.get_Field(i).Editable == true) // dc.ReadOnly = false; //else // dc.ReadOnly = true; dt.Columns.Add(dc); dc = null; } IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false); IFeature pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) { DataRow dr = dt.NewRow(); for (int j = 0; j < pLayerFields.FieldCount; j++) { if (pLayerFields.FindField(pFeatureClass.ShapeFieldName) == j) { dr[j] = pFeatureClass.ShapeType.ToString(); } else { dr[j] =pFeature.get_Value(j); } } dt.Rows.Add(dr); pFeature = pFeatureCursor.NextFeature(); } dataGridView1.DataSource = dt; } catch (Exception exc) { MessageBox.Show("讀取屬性表失敗:" + exc.Message); this.Dispose(); } finally { //this.Close(); } }



更多GIS開發相關問題請加入 GIS開發學習QQ交流群 192251607 共同交流學習!

TOCControl 打開圖層屬性表(轉)