Itable to .net DataGridView Binding

2311
3
Jump to solution
12-26-2012 09:57 PM
shreyesshiv
New Contributor III
I am using ArcGIS 9.3.1.
I want to bind the Itable to a .net DataGridView on framework 3.5.
How do i do it?

Please help

thanks
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
I am using ArcGIS 9.3.1.
I want to bind the Itable to a .net DataGridView on framework 3.5.
How do i do it?

Please help

thanks


You wouldn't bind to the DataGridView.  Rather, you would likely have to actually create and populate a new DATASOURCE of your DataGridView.  that is, create a DataTable from your ITable then bind to the DGV.  Here is a function I have in an implementation that works very well to convert a FeatureLayer into an ADO.NET DataTable object --- you should be able to replace with your ITable instead of the FeatureLayer relatively easily.

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

View solution in original post

0 Kudos
3 Replies
JamesCrandall
MVP Frequent Contributor
I am using ArcGIS 9.3.1.
I want to bind the Itable to a .net DataGridView on framework 3.5.
How do i do it?

Please help

thanks


You wouldn't bind to the DataGridView.  Rather, you would likely have to actually create and populate a new DATASOURCE of your DataGridView.  that is, create a DataTable from your ITable then bind to the DGV.  Here is a function I have in an implementation that works very well to convert a FeatureLayer into an ADO.NET DataTable object --- you should be able to replace with your ITable instead of the FeatureLayer relatively easily.

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

View solution in original post

0 Kudos
shreyesshiv
New Contributor III
Thank you James. It worked perfectly.
I recommend that, this solution strongly works.
cheers
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thank you James. It worked perfectly.
I recommend that, this solution strongly works.
cheers


Glad to hear it helped!
0 Kudos