用过ArcGIS的同志应该都知道它的属性表分页功能,如下图所示:
这次我也照葫芦画瓢,自己做了一个ArcEngine + DevExpress的分页Demo,分页主要使用IQueryFilterDefinition3接口的SetPaginationClause方法,需要注意的是:该接口只对于GDB要素有效,对shp文件则无效。界面效果如下图所示:
MainForm.cs代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace DXApplication1
{
public partial class MainForm : DevExpress.XtraEditors.XtraForm
{
// 要素类
private IFeatureClass featureClass = null;
// 要素数量
private int featureCount = 0;
// 当前页索引
private int pageIndex = 0;
// 每页记录条数
private int pageSize = 0;
// 页数总和
private int pageTotal = 0;
// 构造函数
public MainForm()
{
InitializeComponent();
Init();
}
// 初始化
private void Init()
{
// 获取要素类
this.featureClass = Common.GetFeatureClass();
this.featureCount = featureClass.FeatureCount(null);
// 每页记录条数
RepositoryItemComboBox item = cmbPageSize.Edit as RepositoryItemComboBox;
item.Items.Add(50);
item.Items.Add(100);
item.Items.Add(200);
item.Items.Add(500);
this.cmbPageSize.EditValue = item.Items[0];
// 计算分页参数
this.pageIndex = 1;
this.pageSize = int.Parse(this.cmbPageSize.EditValue.ToString());
this.pageTotal = featureCount % pageSize == 0 ? featureCount / pageSize : featureCount / pageSize + 1;
// 设置标签文本
this.txtCurrentPageIndex.EditValue = pageIndex.ToString();
this.lblPageTotal.Caption = "页,共" + pageTotal.ToString() + "页";
this.lblRowsCount.Caption = "共有" + featureCount.ToString() + "条记录";
this.GetPaginationTable(pageIndex, pageSize);
}
// 切换每页记录条数
private void cmbPageSize_EditValueChanged(object sender, EventArgs e)
{
this.pageIndex = 1;
this.pageSize = int.Parse(this.cmbPageSize.EditValue.ToString());
this.pageTotal = featureCount % pageSize == 0 ? featureCount / pageSize : featureCount / pageSize + 1;
this.txtCurrentPageIndex.EditValue = pageIndex.ToString();
this.lblPageTotal.Caption = "页,共" + pageTotal.ToString() + "页";
this.GetPaginationTable(pageIndex, pageSize);
}
// 第一页
private void btnFirstPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (pageIndex != 1)
{
this.pageIndex = 1;
this.txtCurrentPageIndex.EditValue = pageIndex.ToString();
this.GetPaginationTable(pageIndex, pageSize);
}
}
// 前一页
private void btnPreviousPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (pageIndex != 1)
{
this.pageIndex -= 1;
this.txtCurrentPageIndex.EditValue = pageIndex.ToString();
this.GetPaginationTable(pageIndex, pageSize);
}
}
// 后一页
private void btnNextPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (pageIndex != pageTotal)
{
this.pageIndex += 1;
this.txtCurrentPageIndex.EditValue = pageIndex.ToString();
this.GetPaginationTable(pageIndex, pageSize);
}
}
// 最后一页
private void btnLastPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (pageIndex != pageTotal)
{
this.pageIndex = pageTotal;
this.txtCurrentPageIndex.EditValue = pageIndex.ToString();
this.GetPaginationTable(pageIndex, pageSize);
}
}
// 跳转
private void btnGo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
int go;
if (!int.TryParse(txtTargetPageIndex.EditValue.ToString(), out go))
{
XtraMessageBox.Show("请输入正确的页数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (go < 1 || go > pageTotal)
{
XtraMessageBox.Show("输入的页数必须小于总页数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
this.pageIndex = go;
this.txtCurrentPageIndex.EditValue = pageIndex.ToString();
this.GetPaginationTable(pageIndex, pageSize);
}
// 获取分页数据
private void GetPaginationTable(int offset, int rowsCount)
{
IQueryFilter pQueryFilter = new QueryFilter();
IQueryFilterDefinition3 pQueryFilterDefinition3 = pQueryFilter as IQueryFilterDefinition3;
pQueryFilterDefinition3.SetPaginationClause((offset - 1) * rowsCount, rowsCount);
dgvAttributesTable.DataSource = Common.GetAttributesTable(featureClass, pQueryFilter);
}
}
}
Common.cs代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace DXApplication1
{
public class Common
{
// 获取要素类
public static IFeatureClass GetFeatureClass()
{
IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(@"C:\Users\DSF\Desktop\test.gdb", 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass("data");
return pFeatureClass;
}
// 获取要素属性表
public static DataTable GetAttributesTable(IFeatureClass pFeatureClass, IQueryFilter pQueryFilter)
{
string geometryType = string.Empty;
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
geometryType = "点";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryMultipoint)
{
geometryType = "点集";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
geometryType = "折线";
}
if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
geometryType = "面";
}
// 字段集合
IFields pFields = pFeatureClass.Fields;
int fieldsCount = pFields.FieldCount;
// 写入字段名
DataTable dataTable = new DataTable();
for (int i = 0; i < fieldsCount; i++)
{
dataTable.Columns.Add(pFields.get_Field(i).Name);
}
// 要素游标
IFeatureCursor pFeatureCursor = pFeatureClass.Search(pQueryFilter, true);
IFeature pFeature = pFeatureCursor.NextFeature();
if (pFeature == null)
{
return dataTable;
}
// 获取MZ值
IMAware pMAware = pFeature.Shape as IMAware;
IZAware pZAware = pFeature.Shape as IZAware;
if (pMAware.MAware)
{
geometryType += " M";
}
if (pZAware.ZAware)
{
geometryType += "Z";
}
// 写入字段值
while (pFeature != null)
{
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < fieldsCount; i++)
{
if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
{
dataRow[i] = geometryType;
}
else
{
dataRow[i] = pFeature.get_Value(i).ToString();
}
}
dataTable.Rows.Add(dataRow);
pFeature = pFeatureCursor.NextFeature();
}
// 释放游标
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
return dataTable;
}
}
}