題外話:呵呵,最近在做個項目,對於使用的每個Shp檔案都要瀏覽其屬性工作表,可我使用的每個檔案裡的Feature都達到了五位元以上,用一般的方法瀏覽其屬性工作表實在是太長時間了,於是乎想到了這個介面,在9.2中對這個介面和ADO的串連進行了比較詳細的說明,但是沒有說明怎麼和像DataGridView和DataGrid這樣的控制項進行綁定的問題,對於初學者是個比較頭疼的問題,這裡我給大家兩個個例子僅供參考,呵呵。 一、瀏覽屬性的一般方法(要是檔案裡的Feature數目不多,這個方法還是可行的,控制項採用的是ListView) try { if (layer != null) { ILayer ipLyr = GetLayerByName(layer.Name); IFeatureLayer ipFLayer = ipLyr as IFeatureLayer; ITable ipTable = ipFLayer as ITable; int count = ipTable.RowCount(null); if (count == 0) { MessageBoxEx.Show("該圖層無實體資料可查詢"); return; } frmShowFeatures frmShowF = new frmShowFeatures(ipLyr, mapControl); frmShowF.lblFeaCount.Text = "該圖層共有" + count.ToString() + "個要素"; string strGeoType = ReturnGeoType(ipTable.GetRow(0).Fields.get_Field(1)); for (int j = 0; j < ipTable.Fields.FieldCount; j++) { frmShowF.listViewEx1.Columns.Add(ipTable.Fields.get_Field(j).Name); } for (int i = 0; i < count; i++) { frmShowF.listViewEx1.Items.Add(ipTable.GetRow(i).OID.ToString()); frmShowF.listViewEx1.Items.SubItems.Add(strGeoType); for (int j = 2; j < ipTable.Fields.FieldCount; j++) { frmShowF.listViewEx1.Items.SubItems.Add(ipTable.GetRow(i).get_Value(j).ToString()); } } frmShowF.Owner = this; frmShowF.TopLevel = true; frmShowF.Show(); //Marshal.ReleaseComObject(ipLyr); Marshal.ReleaseComObject(ipFLayer); Marshal.ReleaseComObject(ipTable); } } catch (Exception ee) { MessageBoxEx.Show(ee.Message); } 二、採用IFDOToADOConnection 綁定資料到DataGriView( 這個方法其實有個問題,對於有空間屬性的Shp檔案來說,DataGridView是顯示不了Shape這個欄位的,不過我們要看的是屬性資訊,可以把這個欄位剔除,如果誰有方法能夠解決這個問題,請告知我吧,謝謝哦) ILayer ipLyr = GetLayerByName(layer.Name); IFeatureLayer ipFLayer = ipLyr as IFeatureLayer; ITable ipTable = ipFLayer as ITable; int count = ipTable.RowCount(null); if (count == 0) { MessageBoxEx.Show("該圖層無實體資料可查詢"); return; } frmShowFeatures frmShowF = new frmShowFeatures(ipLyr, mapControl); frmShowF.lblFeaCount.Text = "該圖層共有" + count.ToString() + "個要素"; IFeatureClass ipFClass = ipFLayer.FeatureClass; IDataset ipDataset = ipFClass as IDataset; IWorkspace ipWS = ipDataset.Workspace; IFDOToADOConnection fdoToadoConnection = new FdoAdoConnectionClass(); //ADODB.Connection adoConnection = (ADODB.Connection)fdoToadoConnection.CreateADOConnection(ipWS); ADODB.Connection adoConnection = new ADODB.Connection(); fdoToadoConnection.Connect(ipWS, adoConnection); ADODB.Recordset adoRecordSet = new ADODB.Recordset(); adoRecordSet.Open("Select FID,Area,x,y from " + layer.Name, adoConnection, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockOptimistic, 0); OleDbDataAdapter custDA = new OleDbDataAdapter(); DataTable dtTerritories = new DataTable("Territories"); custDA.Fill(dtTerritories, adoRecordSet); dataGridView1.DataSource = dtTerritories; adoConnection.Close(); adoRecordSet.Close(); adoConnection = null; adoRecordSet = null; |