private Dictionary <string, string> GetQueryResult(IFeatureClassInfo fcInfo, int featureId) { if (fcInfo == null || featureId < 0) { return(null); } IFdeCursor cursor = null; IRowBuffer row = null; try { IDataSourceFactory dsf = new DataSourceFactory(); if (!dsf.HasDataSourceByString(fcInfo.DataSourceConnectionString)) { return(null); } IDataSource ds = dsf.OpenDataSourceByString(fcInfo.DataSourceConnectionString); if (ds == null) { return(null); } IFeatureDataSet fds = ds.OpenFeatureDataset(fcInfo.DataSetName); if (fds == null) { return(null); } IFeatureClass fc = fds.OpenFeatureClass(fcInfo.FeatureClassName); if (fc == null) { return(null); } IQueryFilter filter = new QueryFilter(); filter.WhereClause = fc.FidFieldName + "=" + featureId; if (fc.GetCount(filter) == 0) { return(null); } cursor = fc.Search(filter, false); if (cursor == null) { return(null); } row = cursor.NextRow(); if (row == null) { return(null); } IFieldInfoCollection fiCol = row.Fields; Dictionary <string, string> dict = new Dictionary <string, string>(); for (int i = 0; i < fiCol.Count; i++) { IFieldInfo fi = row.Fields.Get(i); object obj = row.GetValue(fiCol.IndexOf(fi.Name)); if (obj == null) { continue; } string str = ""; switch (fi.FieldType) { case gviFieldType.gviFieldBlob: case gviFieldType.gviFieldUnknown: case gviFieldType.gviFieldGeometry: break; case gviFieldType.gviFieldFloat: case gviFieldType.gviFieldDouble: double d; if (double.TryParse(obj.ToString(), out d)) { str = d.ToString("0.00"); } break; default: str = obj.ToString(); break; } if (!string.IsNullOrEmpty(str.Trim())) { string temp = (string.IsNullOrEmpty(fi.Alias)) ? fi.Name : fi.Alias; dict[temp] = str; } } return(dict); } catch (Exception ex) { return(null); } }
void axRenderControl1_RcMouseClickSelect(IPickResult PickResult, IPoint IntersectPoint, gviModKeyMask Mask, gviMouseSelectMode EventSender) { if (PickResult == null) { return; } this.dataGridView1.Rows.Clear(); this.axRenderControl1.FeatureManager.UnhighlightAll(); rowMap.Clear(); IPickResult pr = PickResult; if (pr.Type != gviObjectType.gviObjectFeatureLayer) { return; } IFeatureLayerPickResult fpr = pr as IFeatureLayerPickResult; IFeatureLayer layer = fpr.FeatureLayer; IFeatureClassInfo cinfo = layer.FeatureClassInfo; if (cinfo.DataSourceConnectionString.Contains("FireBird2x")) { return; } IDataSource ds = (new DataSourceFactory()).OpenDataSourceByString(cinfo.DataSourceConnectionString); IFeatureDataSet dset = ds.OpenFeatureDataset(cinfo.DataSetName); IFeatureClass shpfc = dset.OpenFeatureClass(cinfo.FeatureClassName); IRowBuffer shprow = shpfc.GetRow(fpr.FeatureId); int shpindex = shprow.FieldIndex("Geometry"); IPolygon polygon = shprow.GetValue(shpindex) as IPolygon; if (polygon == null) { return; } IFdeCursor cursor = null; try { foreach (IFeatureClass fc in fcMap.Keys) { IRowBuffer row = null; List <IRowBuffer> list = new List <IRowBuffer>(); ISpatialFilter filter = new SpatialFilter(); filter.Geometry = polygon; filter.SpatialRel = gviSpatialRel.gviSpatialRelEnvelope; filter.GeometryField = "Geometry"; cursor = fc.Search(filter, false); while ((row = cursor.NextRow()) != null) { list.Add(row); } foreach (IRowBuffer r in list) { int geometryIndex = -1; geometryIndex = r.FieldIndex("Geometry"); if (geometryIndex != -1) { int oid = int.Parse(r.GetValue(0).ToString()); this.axRenderControl1.FeatureManager.HighlightFeature(fc, oid, System.Drawing.Color.Red); string fid = ""; IEnvelope env = null; for (int i = 0; i < r.FieldCount; i++) { string fieldName = r.Fields.Get(i).Name; if (r.Fields.Get(i).Name == "oid") { fid = r.GetValue(i).ToString(); } else if (r.Fields.Get(i).Name == "Geometry") { IGeometry geometry = r.GetValue(i) as IModelPoint; env = geometry.Envelope; } } RowObject ro = new RowObject() { FID = fid, FCGUID = fc.Guid.ToString(), FCName = fc.Name, FeatureClass = fc, Envelop = env }; if (!rowMap.ContainsKey(ro.FID + "/" + ro.FCGUID)) { rowMap.Add(ro.FID + "/" + ro.FCGUID, ro); } } } // end of foreach (IRowBuffer r in list) } // end of foreach (IFeatureClass fc in fcMap.Keys) this.Text = "分析完成!"; LoadGridView(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (cursor != null) { cursor.Dispose(); cursor = null; } } }