我们知道表里面的记录是按照时间的顺序创建的,如果要对某一字段排序,ArcGIS的字段上点右键,但是当我们重新打开后,发现没有变化。写了断小代码可以对表的字段排序,没有对要素类排序,可以参考和修改代码
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 ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
namespace TableSort
{
public partial class Sort : Form
{
private ITable pTable;
IMap pMap;
public Sort(IMap _pMap)
{
InitializeComponent();
this.pMap = _pMap;
}
private void Sort_Load(object sender, EventArgs e)
{
IDataset pDataset = null;
ITable plTable=null ;
ITableCollection pTableCollection = pMap as ITableCollection;
for (int i = 0; i < pTableCollection.TableCount; i++)
{
plTable = pTableCollection.get_Table(i);
pDataset = plTable as IDataset;
comboBox1.Items.Add(pDataset.Name);
}
comboBox1.Text = comboBox1.Items[0].ToString();
FieldName(comboBox2, FindTable (pMap as ITableCollection ,comboBox1.Text ));
comboBox3.Items.Add("升序");
comboBox3.Items.Add("降序");
comboBox3.Text = comboBox3.Items[0].ToString();
}
private void FieldName(ComboBox _pComBox, ITable _pTable)
{
IFields pTableFields = _pTable.Fields;
for (int i = 0; i < pTableFields.FieldCount; i++)
{
if ( pTableFields.get_Field(i).Type != esriFieldType.esriFieldTypeOID)
{
_pComBox.Items.Add(pTableFields.get_Field(i).Name);
}
}
_pComBox.Text = _pComBox.Items[0].ToString();
}
private ITable FindTable(ITableCollection _pTabbleCollection, string _Name)
{
ITable plTable = null; ;
IDataset plDataset;
for (int i = 0; i < _pTabbleCollection .TableCount ;i++)
{
plDataset = _pTabbleCollection.get_Table(i) as IDataset;
if (plDataset.Name == _Name.Trim ())
{
plTable = _pTabbleCollection.get_Table(i);
break;
}
}
return plTable;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FieldName(comboBox2, FindTable(pMap as ITableCollection, comboBox1.Text));
}
private void bOK_Click(object sender, EventArgs e)
{
pTable = FindTable(pMap as ITableCollection, comboBox1.Text);
ITableSort plTalbeSort = new TableSortClass();
plTalbeSort.Table = pTable;
plTalbeSort.Fields = comboBox2.Text.Trim();
if (comboBox3.Text.Trim () == "降序")
{
plTalbeSort.set_Ascending(comboBox2.Text, false );
}
else
{
plTalbeSort.set_Ascending(comboBox2.Text, true);
}
plTalbeSort.Sort(null);
ICursor plSortCursor = plTalbeSort.Rows;
IRow pSortRow = plSortCursor .NextRow ();
IDataset plSortDataset = pTable as IDataset;
IFeatureWorkspace plFWS = plSortDataset.Workspace as IFeatureWorkspace;
ITable plStable= plFWS.CreateTable("NewSort", pTable.Fields, null, null, null);
while (pSortRow != null)
{
IRow pRow = plStable.CreateRow();
for (int i=0;i <pRow .Fields .FieldCount;i++ )
{
if (pRow .Fields .get_Field (i).Type !=esriFieldType .esriFieldTypeOID)
{
pRow.set_Value (i,pSortRow .get_Value (i));
}
}
pRow.Store();
pSortRow = plSortCursor.NextRow();
}
ITableCollection pTC = pMap as ITableCollection;
pTC.AddTable(plStable);
}
}
}