Setting Index of an Attribute

351
1
Jump to solution
12-01-2022 01:35 AM
Meroni
by
New Contributor III

Hi, is it possible to change the order of attributes in a table? If not, how would I go about cloning a table into a new one, but with reordered attributes?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Aashis
by Esri Contributor
Esri Contributor

Hi @Meroni

You cannot modify the order of table attributes at the database level, but you can change them at the LAYERS level at any time.

To modify the attributes' order at the database level, we recommend to clone and reorder the original attributes into a new table as 

private static void CloneAndReorderFields(Geodatabase geodatabase, TableDefinition oldTableDefinition)
{
  SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);
  
  // Old table description
  TableDescription oldTableDescription = new TableDescription(oldTableDefinition);
  
  // Old table's field descriptions
  List<FieldDescription> oldTableFieldDescriptions = new List<FieldDescription>(oldTableDescription.FieldDescriptions);

  // Reordered field descriptions
  List<FieldDescription> reorderedFieldDescriptions = new List<FieldDescription>()
  {
	// Change the order of attribute fields from index 2, 1, 0 (old) to 0, 1, 2 in the new table 
	oldTableFieldDescriptions[2], oldTableFieldDescriptions[1], oldTableFieldDescriptions[0]  	
  };

  // Creating a new reordered table
  TableDescription reorderdTableDescription = new TableDescription($"{oldTableDefinition.GetName()}_Reorder", reorderedFieldDescriptions);
  
  // Build the new table
  schemaBuilder.Create(reorderdTableDescription);
  bool buildStatus= schemaBuilder.Build();
}

View solution in original post

1 Reply
Aashis
by Esri Contributor
Esri Contributor

Hi @Meroni

You cannot modify the order of table attributes at the database level, but you can change them at the LAYERS level at any time.

To modify the attributes' order at the database level, we recommend to clone and reorder the original attributes into a new table as 

private static void CloneAndReorderFields(Geodatabase geodatabase, TableDefinition oldTableDefinition)
{
  SchemaBuilder schemaBuilder = new SchemaBuilder(geodatabase);
  
  // Old table description
  TableDescription oldTableDescription = new TableDescription(oldTableDefinition);
  
  // Old table's field descriptions
  List<FieldDescription> oldTableFieldDescriptions = new List<FieldDescription>(oldTableDescription.FieldDescriptions);

  // Reordered field descriptions
  List<FieldDescription> reorderedFieldDescriptions = new List<FieldDescription>()
  {
	// Change the order of attribute fields from index 2, 1, 0 (old) to 0, 1, 2 in the new table 
	oldTableFieldDescriptions[2], oldTableFieldDescriptions[1], oldTableFieldDescriptions[0]  	
  };

  // Creating a new reordered table
  TableDescription reorderdTableDescription = new TableDescription($"{oldTableDefinition.GetName()}_Reorder", reorderedFieldDescriptions);
  
  // Build the new table
  schemaBuilder.Create(reorderdTableDescription);
  bool buildStatus= schemaBuilder.Build();
}