Shapefile Arribute Table to Datagridview

4958
5
07-30-2012 01:22 PM
BernardoG_
New Contributor II
Can you please let me know (or point me to a tutorial?) how to display a shapefile attribute table in C# DataGridView?

Thanks
0 Kudos
5 Replies
JamesCrandall
MVP Frequent Contributor
I am uncertain of any specific ArcObjects that will do what you want.  What I do is simply create an ADO.NET DataTable object from the FeatureLayer then its just a matter of setting your DataGridView.DataSource to this.  Here's a function that you can pass in your IFeatureLayer and it will return the DataTable.

This is in VB.NET, so you will have to convert to C#

Public Function ConvertToADONETDataTableFromLayer(ByVal inLayer As IFeatureLayer) As DataTable

        Try

            Dim tmpDT As New DataTable("tmpDT")
            Dim column As DataColumn

            Dim pTable As ITable = inLayer.FeatureClass
            Dim pFields As IFields = pTable.Fields
            Dim pCur As ICursor = pTable.Search(Nothing, False)

            For c = 0 To pCur.Fields.FieldCount - 1
                column = New DataColumn()
                column.ColumnName = (pFields.Field(c).Name)
                If pFields.Field(c).Type = esriFieldType.esriFieldTypeString Then
                    column.DataType = System.Type.GetType("System.String")
                ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeInteger Then
                    column.DataType = System.Type.GetType("System.Int32")
                ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeDouble Then
                    column.DataType = System.Type.GetType("System.Double")
                ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeDate Then
                    column.DataType = System.Type.GetType("System.DateTime")
                ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeSingle Then
                    column.DataType = System.Type.GetType("System.Single")
                ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeBlob Then
                    column.DataType = System.Type.GetType("System.Byte")
                End If


                column.ReadOnly = False
                tmpDT.Columns.Add(column)
            Next

            Dim pRow As IRow = pCur.NextRow
            Dim newRow As DataRow

            Do Until pRow Is Nothing

                newRow = tmpDT.NewRow()
                newRow.BeginEdit()
                For cols = 0 To pCur.Fields.FieldCount - 1
                    newRow(cols) = pRow.Value(pRow.Fields.FindField(pFields.Field(cols).Name))
                Next
                newRow.EndEdit()
                tmpDT.Rows.Add(newRow)
                tmpDT.AcceptChanges()

                pRow = pCur.NextRow

            Loop

            Return tmpDT

        Catch ex As Exception
            MsgBox(ex.ToString)
            Return Nothing
        End Try

    End Function
0 Kudos
sameerpuppal
Occasional Contributor
Hi,

Above post converts the table data to .net datatable which seems to me is nice and easy way. but I believe we can avoid ICursors and looping through the IRow.

There is a better way to do it, and this is how to do it

use ESRI.ArcGIS.Utility.Converter.ToDataSet.


Sample code:

ITable table = featureWorkspace.OpenTable(tableName);
IRecordSetInit recordSetInit= new ESRI.ArcGIS.Geodatabase.RecordSetClass();
recordSetInit.SetSourceTable(table, new QueryFilterClass());
IRecordSet recordSet= recordSetInit as IRecordSet;

System.Data.DataSet netDS = ESRI.ArcGIS.Utility.Converter.ToDataSet(recordSet);


I believe this will reduce the efforts and time required for looping through cursors.
after getting Datatable you can easily bind it with the Datagrid.

Hope this helps!

Regards,
Sameer
0 Kudos
JamesCrandall
MVP Frequent Contributor
Sameer,

What version of ArcGIS are you running this utility on?  I had seen this a while ago but did not pursue it because I was under the impression that this utility was moved to Server.WebControls namespace and is unavailable for ArcGIS desktop solutions.

Does this work for ArcGIS 10.x?



Hi,

Above post converts the table data to .net datatable which seems to me is nice and easy way. but I believe we can avoid ICursors and looping through the IRow.

There is a better way to do it, and this is how to do it

use ESRI.ArcGIS.Utility.Converter.ToDataSet.


Sample code:

ITable table = featureWorkspace.OpenTable(tableName);
IRecordSetInit recordSetInit= new ESRI.ArcGIS.Geodatabase.RecordSetClass();
recordSetInit.SetSourceTable(table, new QueryFilterClass());
IRecordSet recordSet= recordSetInit as IRecordSet;

System.Data.DataSet netDS = ESRI.ArcGIS.Utility.Converter.ToDataSet(recordSet);


I believe this will reduce the efforts and time required for looping through cursors.
after getting Datatable you can easily bind it with the Datagrid.

Hope this helps!

Regards,
Sameer
0 Kudos
LeoDonahue
Occasional Contributor III
0 Kudos
sameerpuppal
Occasional Contributor
Sameer,

What version of ArcGIS are you running this utility on?  I had seen this a while ago but did not pursue it because I was under the impression that this utility was moved to Server.WebControls namespace and is unavailable for ArcGIS desktop solutions.

Does this work for ArcGIS 10.x?


I was using the code on 9.X i guess this class has deprecated and don't know new interface which has similar method! will check and let you know!
0 Kudos