reading values in selected row (c#)

4370
1
05-21-2012 03:12 PM
InsuHong1
New Contributor
Hi,

Is there any way to read values in selected records in c#?

What I want to is:

1. select some portion of feature A
2. read field value in selected rows in feature A

Cursor object only to through 1st to last, not portionly.

So now I copied selected row into new feature class, and then make cursor for that copied feature. But it takes more time...

Is there any faster way to do this?


thanks.
0 Kudos
1 Reply
DennisGeasan
Occasional Contributor II
Not sure if this will help but maybe.  To use feature class fields in a .Net object you have to convert an ITable object to a .Net DataTable.  The attached code should do that for you.  Note that you have to determine the data types in the feature class and create fields of the same or similar types in the DataTable.  The attached code only does the matching for some data types but the pattern should be a guide.

To get the iTable object:

IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pGDBWorkSpace;
IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(m_SDEDB_FeatureClassName);
ITable pFCAttributeTable = (ITable)pFeatureClass;
DT = ConvertITable(pFCAttributeTable, SortFields);


This does the work:

private DataTable ConvertITable(ITable table, string SortFields)
{
 //Dennis Geasan - March 1, 2012
 //Convert an ITable object to a .Net DataTable Object.
 //
 //Found the original code on ESRI Forums
 //http://forums.esri.com/Thread.asp?c=159&f=1707&t=209915
 //wang chisheng  Date Jan 18, 2007  
 //
 //The following may also be relavent for future work.
 //http://edndoc.esri.com/arcobjects/9.2/NET/a6eb84fb-93db-4bc3-82a9-874d8890a8ca.htm
 //
 DataTable DatatableFromITable;
 DatatableFromITable = new DataTable("DatatableFromITable");

 try
 {
  //First sort the table then convert that to a DataTable.
  // Create the TableSort object.
  ITableSort tableSort = new TableSortClass();
  tableSort.Table = table;
  tableSort.Fields = SortFields;
  tableSort.set_Ascending(SortFields, true);
  tableSort.Sort(null);

  IQueryFilter que = new QueryFilterClass();
  ICursor pCursor = tableSort.Rows;
  IRow pRow = pCursor.NextRow();

  if (pRow != null)
  {
   //For this loop I need to define the data type on the column being added to the DataTable.
   //Get this from pRow.Fields.Field.Type.
   //You  have to map every ESRI data type to .Net data types.  Note that this has not been done for all field types.  Modify as needed.
   for (int i = 0; i < pRow.Fields.FieldCount; i++)
   {
    //Add all of the FC columns to the DataTable object.
    DatatableFromITable.Columns.Add(pRow.Fields.get_Field(i).Name);

    //Determine the ESRI data type of the incoming fields.  
    //This code example is skipping some types
    if (pRow.Fields.Field.Type != esriFieldType.esriFieldTypeBlob && pRow.Fields.Field.Type != esriFieldType.esriFieldTypeGeometry && pRow.Fields.Field.Type != esriFieldType.esriFieldTypeGlobalID && pRow.Fields.Field.Type != esriFieldType.esriFieldTypeGUID && pRow.Fields.Field.Type != esriFieldType.esriFieldTypeOID && pRow.Fields.Field.Type != esriFieldType.esriFieldTypeRaster && pRow.Fields.Field.Type != esriFieldType.esriFieldTypeXML)
    {
     switch (pRow.Fields.Field.Type)
     {
      case esriFieldType.esriFieldTypeString:
       DatatableFromITable.Columns[pRow.Fields.get_Field(i).Name].DataType = System.Type.GetType("System.String");
       break;

      case esriFieldType.esriFieldTypeDate:
       DatatableFromITable.Columns[pRow.Fields.get_Field(i).Name].DataType = System.Type.GetType("System.DateTime");
       break;

      case esriFieldType.esriFieldTypeDouble: 
       DatatableFromITable.Columns[pRow.Fields.get_Field(i).Name].DataType = System.Type.GetType("System.Double");
       break;

      case esriFieldType.esriFieldTypeInteger:
       DatatableFromITable.Columns[pRow.Fields.get_Field(i).Name].DataType = System.Type.GetType("System.Int32");
       break;

      case esriFieldType.esriFieldTypeSingle:
       DatatableFromITable.Columns[pRow.Fields.get_Field(i).Name].DataType = System.Type.GetType("System.Single");
       break;

      case esriFieldType.esriFieldTypeSmallInteger:
       DatatableFromITable.Columns[pRow.Fields.get_Field(i).Name].DataType = System.Type.GetType("System.Int16");
       break;

      default:
       break;
     }
    }
   }
   //Now copy the data to a .Net DataRow and then a DataTable
   while (pRow != null)
   {
    DataRow pDataRow = DatatableFromITable.NewRow();
    for (int j = 0; j < pCursor.Fields.FieldCount; j++)
     pDataRow = pRow.get_Value(j);
    DatatableFromITable.Rows.Add(pDataRow);
    pRow = pCursor.NextRow();
   }
  }
 }
 catch (System.Exception ex)
 {
  //MessageBox.Show(ex.Message);
  m_Error = true;
  m_ErrorMessage = "Something went wrong while converting from ITable to DataTable.\n" + ex.Message;
 }
 //Apparently the following doesn't really sort the table.  A DataView object derived from DataTable.DataView will be sorted.
 //DatatableFromITable.DefaultView.Sort = SortFields;
 return DatatableFromITable;
}
0 Kudos