




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
ArcGISEngine+C#實(shí)例開發(fā)教程》第八講屬性數(shù)據(jù)表的查詢顯示時(shí)間:2009-05-0106:31:26來源:3SDN.Net作者:3SDN原創(chuàng)點(diǎn)擊量:1640在上一講中,我們完成了圖層符號(hào)選擇器的制作。這一講中,我們將實(shí)現(xiàn)圖層屬性數(shù)據(jù)表的查詢顯示。在ArcMap中,單擊圖層右鍵菜單中的“OpenAttributeTable”命令,便可彈出屬性數(shù)據(jù)表。本講將完成類似的功能,效果如下:圖1數(shù)據(jù)表顯示,我們用了DataGridView控件。DataGridView控件提供一種強(qiáng)大而靈活的以表格形式顯示數(shù)據(jù)的方式??梢允褂肈ataGridView控件來顯示少量數(shù)據(jù)的只讀視圖,也可以對(duì)其進(jìn)行縮放以顯示特大數(shù)據(jù)集的可編輯視圖。我們可以很方便地把一個(gè)DataTable作為數(shù)據(jù)源綁定到DataGridView控件中。本講的思路大體如下:首先根據(jù)圖層屬性中的字段創(chuàng)建一個(gè)空的DataTable,然后根據(jù)數(shù)據(jù)內(nèi)容一行行填充DataTable數(shù)據(jù),再將DataTable綁定到DataGridView控件,最后調(diào)用并顯示屬性表窗體。1.創(chuàng)建屬性表窗體新建一個(gè)Windows窗體,命名為“AttributeTableFrm.cs”。從工具箱拖一個(gè)DataGridView控件到窗體,并將其Dock屬性設(shè)置為“Fill”。添加如下引用:usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.SystemUI;usingESRI.ArcGIS.Geometry;usingESRI.ArcGIS.Geodatabase;2.創(chuàng)建空DataTable首先傳入ILayer,再查詢到ITable,從ITable中的Fileds中獲得每個(gè)Field,再根據(jù)Filed設(shè)置DataTable的DataColumn,由此創(chuàng)建一個(gè)只含圖層字段的空DataTable。實(shí)現(xiàn)函數(shù)如下:///<summary>/〃根據(jù)圖層字段創(chuàng)建一個(gè)只含字段的空DataTable///</summary>///<paramname="pLayer"></param>///<paramname="tableName"></param>///<returns></returns>privatestaticDataTableCreateDataTableByLayer(ILayerpLayer,stringtableName){〃創(chuàng)建一個(gè)DataTable表DataTablepDataTable=newDataTable(tableName);〃取得ITable接口ITablepTable=pLayerasITable;IFieldpField=null;DataColumnpDataColumn;〃根據(jù)每個(gè)字段的屬性建立DataColumn對(duì)象for(inti=0;i<pTable.Fields.FieldCount;i++){pField=pTable.Fields.get_Field(i);//新建一個(gè)DataColumn并設(shè)置其屬性pDataColumn=newDataColumn(pField.Name);if(pField.Name==pTable.OIDFieldName){pDataColumn.Unique=true;//字段值是否唯一}//字段值是否允許為空pDataColumn.AllowDBNull=pField.IsNullable;//字段別名pDataColumn.Caption=pField.AliasName;//字段數(shù)據(jù)類型pDataColumn.DataType=System.Type.GetType(ParseFieldType(pField.Type));//字段默認(rèn)值pDataColumn.DefaultValue=pField.DefaultValue;〃當(dāng)字段為String類型是設(shè)置字段長度if(pField.VarType==8){pDataColumn.MaxLength=pField.Length;}//字段添加到表中pDataTable.Columns.Add(pDataColumn);pField=null;pDataColumn=null;}returnpDataTable;}因?yàn)镚eoDatabase的數(shù)據(jù)類型與.NET的數(shù)據(jù)類型不同,故要進(jìn)行轉(zhuǎn)換。轉(zhuǎn)換函數(shù)如下:///<summary>///將GeoDatabase字段類型轉(zhuǎn)換成.Net相應(yīng)的數(shù)據(jù)類型///</summary>///vparam皿皿6="矗1小丁『卩6">字段類型</param>///<returns></returns>publicstaticstringParseFieldType(esriFieldTypefieldType){switch(fieldType){caseesriFieldType.esriFieldTypeBlob:return"System.String";caseesriFieldType.esriFieldTypeDate:return"System.DateTime";caseesriFieldType.esriFieldTypeDouble:return"System.Double";caseesriFieldType.esriFieldTypeGeometry:return"System.String";caseesriFieldType.esriFieldTypeGlobalID:return"System.String";caseesriFieldType.esriFieldTypeGUID:return"System.String";caseesriFieldType.esriFieldTypeInteger:return"System.Int32";caseesriFieldType.esriFieldTypeOID:return"System.String";caseesriFieldType.esriFieldTypeRaster:return"System.String";caseesriFieldType.esriFieldTypeSingle:return"System.Single";caseesriFieldType.esriFieldTypeSmallInteger:return"System.Int32";caseesriFieldType.esriFieldTypeString:return"System.String";default:return"System.String";}}本篇文章來源于3SDN轉(zhuǎn)載請(qǐng)以鏈接形式注明出處網(wǎng)址:/gis2dev/ae/2009-05-01/440.html3.裝載DataTable數(shù)據(jù)從上一步得到的DataTable還沒有數(shù)據(jù),只有字段信息。因此,我們要通過ICursor從ITable中逐一取出每一行數(shù)據(jù),即IRow。再創(chuàng)建DataTable中相應(yīng)的DataRow,根據(jù)IRow設(shè)置DataRow信息,再將所有的DataRow添加到DataTable中,就完成了DataTable數(shù)據(jù)的裝載。為保證效率,一次最多只裝載2000條數(shù)據(jù)到DataGridView。函數(shù)代碼如下:///<summary>///填充DataTable中的數(shù)據(jù)///v/summary>///<paramname="pLayer"></param>///<paramname="tableName"></param>///<returns></returns>publicstaticDataTableCreateDataTable(ILayerpLayer,stringtableName){//創(chuàng)建空DataTableDataTablepDataTable=CreateDataTableByLayer(pLayer,tableName);//取得圖層類型stringshapeType=getShapeType(pLayer);//創(chuàng)建DataTable的行對(duì)象DataRowpDataRow=null;//從ILayer查詢到ITableITablepTable=pLayerasITable;ICursorpCursor=pTable.Search(null,false);〃取得ITable中的行信息IRowpRow=pCursor.NextRow();intn=0;while(pRow!=null){//新建DataTable的行對(duì)象pDataRow=pDataTable.NewRow();for(inti=0;i<pRow.Fields.FieldCount;i++){〃如果字段類型為esriFieldTypeGeometry,則根據(jù)圖層類型設(shè)置字段值if(pRow.Fields.get_Field(i).Type==esriFieldType.esriFieldTypeGeometry){pDataRow[i]=shapeType;}〃當(dāng)圖層類型為Anotation時(shí),要素類中會(huì)有esriFieldTypeBlob類型的數(shù)據(jù),〃其存儲(chǔ)的是標(biāo)注內(nèi)容,如此情況需將對(duì)應(yīng)的字段值設(shè)置為Elementelseif(pRow.Fields.get_Field(i).Type==esriFieldType.esriFieldTypeBlob){pDataRow[i]="Element";}else{pDataRow[i]=pRow.get_Value(i);}}//添加DataRow到DataTablepDataTable.Rows.Add(pDataRow);pDataRow=null;n++;//為保證效率,一次只裝載最多條記錄if(n==2000){pRow=null;}else{pRow=pCursor.NextRow();returnpDataTable;}上面的代碼中涉及到一個(gè)獲取圖層類型的函數(shù)getShapeTape,此函數(shù)是通過ILayer判斷圖層類型的,代碼如下:///<summary>/〃獲得圖層的Shape類型///v/summary>///vparamname="pLayer">圖層</param>///<returns></returns>publicstaticstringgetShapeType(ILayerpLayer){IFeatureLayerpFeatLyr=(IFeatureLayer)pLayer;switch(pFeatLyr.FeatureClass.ShapeType){caseesriGeometryType.esriGeometryPoint:return"Point";caseesriGeometryType.esriGeometryPolyline:return"Polyline";caseesriGeometryType.esriGeometryPolygon:return"Polygon";default:return}}4.綁定DataTable到DataGridView通過以上步驟,我們已經(jīng)得到了一個(gè)含有圖層屬性數(shù)據(jù)的DataTable。現(xiàn)定義一個(gè)AttributeTableFrm類的成員變量:publicDataTableattributeTable;通過以下函數(shù),我們很容易將其綁定到DataGridView控件中。///<summary>/〃綁定DataTable到DataGridView///</summary>///<paramname="player"></param>publicvoidCreateAttributeTable(ILayerplayer){stringtableName;tableName=getValidFeatureClassName(player.Name);attributeTable=CreateDataTable(player,tableName);this.dataGridView1.DataSource=attributeTable;this.Text="屬性表["+tableName+"]"+"記錄數(shù):"+attributeTable.Rows.Count.ToString();}因?yàn)镈ataTable的表名不允許含有“?”因此我們用“_”替換。函數(shù)如下:///<summary>///替換數(shù)據(jù)表名中的點(diǎn)///</summary>///<paramname="FCname"></param>///<returns></returns>publicstaticstringgetValidFeatureClassName(stringFCname){intdot=FCname.IndexOf(".");if(dot!=-1){returnFCname.Replace(".","_");}returnFCname;}本篇文章來源于3SDN轉(zhuǎn)載請(qǐng)以鏈接形式注明出處網(wǎng)址:/gis2dev/ae/2009-05-01/4402.html5.調(diào)用屬性表窗體通過1-4步驟,我們封裝了一個(gè)AttributeTableFrm類,此類能夠由ILayer顯示圖層中的屬性表數(shù)據(jù)。那怎么調(diào)用AttributeTableFrm呢?前面已經(jīng)提到,我們是在TOCControl選中圖層的右鍵菜單中彈出屬性表窗體的,因此我們需要添加一個(gè)菜單項(xiàng)到TOCControl中Layer的右鍵菜單。而在第六講中,我們采用的是AE中的IToolbarMenu實(shí)現(xiàn)右鍵菜單的,故我們還需自定義一個(gè)Command,實(shí)現(xiàn)打開屬性表的功能。以ArcGIS的BaseCommand為模板新建項(xiàng)“OpenAttributeTable.cs”注意:新建BaseCommand模板時(shí),會(huì)彈出一個(gè)對(duì)話框讓我們選擇模板適用對(duì)象,這時(shí)我們要選擇MapControl、PageLayoutControl,即選擇第二項(xiàng)或者倒數(shù)第二項(xiàng)。添加如下引用:usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Display;usingESRI.ArcGIS.esriSystem;添加成員變量:privateILayerm_pLayer;修改構(gòu)造函數(shù)為:publicOpenAttributeTable(ILayerpLayer){////TODO:Definevaluesforthepublicproperties//base.m_category="";//localizabletextbase.m_caption="打開屬性表";//localizabletextbase.m_message="打開屬性表";//localizabletextbase.m_toolTip="打開屬性表";//localizabletextbase.m_name="打開屬性表";//uniqueid,non-localizable(e.g."MyCategory_MyCommand")m_pLayer=pLayer;try////TODO:changebitmapnameifnecessary//stringbitmapResourceName=GetType().Name+".bmp";base.m_bitmap=newBitmap(GetType(),bitmapResourceName);}catch(Exceptionex){System.Diagnostics.Trace.WriteLine(ex.Message,"InvalidBitmap");}}再在On_Click函數(shù)中添加如下代碼,以創(chuàng)建并打開屬性表窗體。///<summary>///Occurswhenthiscommandisclicked///</summary>publicoverridevoidOnClick(){//TODO:AddOpenAttributeTable.OnClickimplementationAttributeTableFrmattributeTable=newAttributeTableFrm();attributeTable.CreateAttributeTable(m_pLayer);attributeTable.ShowDialog();}至此,我們完成了OpenAttributeTable命令。顯然,我們要在TOCControl的OnMouseDown事件中調(diào)用此命令。因?yàn)椋?dāng)前選中的圖層參數(shù),即ILayer是通過OpenAttributeTable的構(gòu)造函數(shù)傳入的,而選中的ILayer是動(dòng)態(tài)變化的,所以我們無法在窗體初始化的Form1_Load事件中就添加OpenAttributeTable菜單項(xiàng)到右鍵菜單。但我們可以在OnMouseDown事件中動(dòng)態(tài)添加OpenAttributeTable菜單項(xiàng)。要注意的是,最后我們必須移除添加的OpenAttributeTable
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2020-2021深圳沙井立才學(xué)校小學(xué)三年級(jí)數(shù)學(xué)下期末第一次模擬試題帶答案
- 施工現(xiàn)場臨電施工方案
- 沖孔模具施工方案范本
- 小學(xué)課本劇一年級(jí)《雪孩子》-劇本
- 2025年中考物理二輪復(fù)習(xí):聲光專題 能力提升練習(xí)題(含答案解析)
- 2024年廣東省中考滿分作文《當(dāng)好自己故事的主角》2
- 第八單元 課題1金屬材料教學(xué)設(shè)計(jì)-2024-2025九年級(jí)化學(xué)人教版2024下冊(cè)
- 第2課 產(chǎn)生氣體的變化(教學(xué)設(shè)計(jì))-2023-2024學(xué)年六年級(jí)下冊(cè)科學(xué) 教科版
- 合同范本政府土地使用
- 農(nóng)作物賠償合同范例
- me實(shí)驗(yàn)2 電位、電壓的測定及電路電位圖的繪制
- EGCs與腸道微環(huán)境相互作用的研究進(jìn)展
- 特殊兒童隨班就讀申請(qǐng)書范本
- 三年級(jí)下冊(cè)英語教材解讀-教材解讀|魯科版(五四學(xué)制)(三起)
- 道路施工導(dǎo)改及施工方案
- 《實(shí)數(shù)》單元作業(yè)設(shè)計(jì)
- (word完整版)教師個(gè)人簡歷模板
- 專題11 以小見大-【幫作文】初中語文之從課文中學(xué)習(xí)寫作 課件(共25張PPT)
- 互聯(lián)網(wǎng)公司勞動(dòng)合同
- 吉美版四年級(jí)綜合實(shí)踐活動(dòng)下冊(cè)全冊(cè)表格簡約式教案教學(xué)設(shè)計(jì)
- 2023河南對(duì)口高考計(jì)算機(jī)類基礎(chǔ)課試題
評(píng)論
0/150
提交評(píng)論