最近在研究ArcGlobe开发,写了个小程序,其中有一个功能是在ArcGlobe中进行数据查询,现在把主要代码贴出来。
/// <summary>
/// 查询功能
/// </summary>
/// <paramname=”sender”></param>
/// <paramname=”e”></param>
private void BtnOK_Click(objectsender, EventArgs e)
{
try
{
//图层名称
String LayerName = this.CbxLayer.Text;
//查询条件
String SearchText = this.TbxName.Text;
if (SearchText == String.Empty)
{
MessageBox.Show(“查询条件不允许为空!”, “错误”, MessageBoxButtons.OK, MessageBoxIcon.Error);
TbxName.Focus();
//清空数据列表
this.dg1.DataSource = null;
//跳出查询
return;
}
//当前视图
IScene scene = this.axGlobeControl1.Globe.GlobeDisplay.Scene;
//查询器
IQueryFilter pQueryFilter = new QueryFilterClass();
IActiveView pActiveView;
pActiveView = (IActiveView)scene;
//清空当前视图
scene.ClearSelection();
IFeatureLayer pFeatureLayer;
pFeatureLayer = (IFeatureLayer)scene.get_Layer(GetLayerId(LayerName, scene));
//判断要查询的图层
if (LayerName == “堤防”)
{
//查询条件
pQueryFilter.WhereClause = “XMMC like “%” + SearchText + “%”";
}
else if (LayerName == “险工险段”)
{
//查询条件
pQueryFilter.WhereClause = “BZ like “%” + SearchText + “%”";
}
……
IFeatureCursor pFeatureCursor;
//查询
pFeatureCursor = pFeatureLayer.FeatureClass.Search(pQueryFilter, false);
IFeature pFeature;
pFeature = pFeatureCursor.NextFeature();
IFields pFields = pFeatureCursor.Fields;
int fieldIndex = 0;
int fieldIndexName = 0;
if (LayerName == “堤防”)
{
fieldIndex = pFields.FindField(“NAME”);
fieldIndexName = pFields.FindField(“XMMC”);
}
else if (LayerName == “险工险段”)
{
fieldIndex = pFields.FindField(“NAME”);
fieldIndexName = pFields.FindField(“BZ”);
}
……
pfeat = pFeature;
if (pFeature == null)
{
MessageBox.Show(“您所查询的结果不存在!”);
TbxName.Select();
TbxName.Focus();
this.dg1.DataSource = null;
return;
}
//绑定DataTable
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn();
dc1.ColumnName = “工程代码”;
DataColumn dc2 = new DataColumn();
dc2.ColumnName = “工程名称”;
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
string Code = String.Empty;
string Name = String.Empty;
while (pFeature != null)
{
//scene.SelectFeature(pFeatureLayer, pFeature);
Code = pFeature.get_Value(fieldIndex) as string;
Name = pFeature.get_Value(fieldIndexName) as string;
DataRow dr = dt.NewRow();
dr[0] = Code;
dr[1] = Name;
dt.Rows.Add(dr);
pFeature = pFeatureCursor.NextFeature();
}
//绑定列表
this.dg1.DataSource = dt.DefaultView;
}
catch
{
MessageBox.Show(“异常!”, “错误”, MessageBoxButtons.OK, MessageBoxIcon.Error);
this.dg1.DataSource = null;
}
}