Bind attribute field to drop down list

2571
13
05-20-2010 12:59 AM
NehayaArabeiat
New Contributor
Hi,

How can I bind attribute field to drop down list in .net application.

Thanks alot
0 Kudos
13 Replies
JamesCrandall
MVP Frequent Contributor
Hi,

How can I bind attribute field to drop down list in .net application.

Thanks alot


You will have to be a bit more specific in what you would like to do, as well as the project type (a .NET application can be many different things).  Isn't a "DropDownList" is an ASP.NET control (not totally sure on that)?  Or are you referring to a ComboBox Control that is placed on a Windows Form or UserControl?

I'm not a immediately aware of any specific methods that allow you to bind fields from an attribute table to an ASP.NET DropDownList control, but I'm sure there is a solution to get it done.
0 Kudos
NehayaArabeiat
New Contributor
Yes you are right I building ArcGIS windows application and it is a combobox not a dropdownlist.

Thanks for your reply
0 Kudos
JamesCrandall
MVP Frequent Contributor
Yes you are right I building ArcGIS windows application and it is a combobox not a dropdownlist.

Thanks for your reply


This seems to be a popular topic lately 🙂

Here is a recent thread (as of yesterday) where someone is populating a ComboBox Control with the unique values of a particular layer's attribute field:

http://forums.arcgis.com/threads/4766-DataStatistics-Interface-and-System.Collection.IEnumerator

While this is not necessarily a "DataBinding" solution, it should at least get you started. 

Good luck!
0 Kudos
NehayaArabeiat
New Contributor
Thanks james but can I use it to access data from file geodatabase not from map layers.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks james but can I use it to access data from file geodatabase not from map layers.


If the GDB FeatureClass is loaded in ArcMap's TOC, then just set pTable (from the code sample in the other thread) to this.  Otherwise, you will have to setup an IFeatureWorkspace and specify the GDB that contains the Tables/FeatureClasses.

http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeoDatabase/IFeatureWorkspace.htm
0 Kudos
NehayaArabeiat
New Contributor
I have setup FileGDBWorkspaceFactory and define where the geodatabase file path but I how can I get the feature layer from it ??
0 Kudos
JamesCrandall
MVP Frequent Contributor
I have setup FileGDBWorkspaceFactory and define where the geodatabase file path but I how can I get the feature layer from it ??


I'm away from my dev workstation at the moment, but I think it's something like this:

Dim pTable As ITable
pTable = pFeatureWorkspace.OpenTable("Feature Class Name Goes here")


Or if it's a Layer I think you want something like this:

Dim pFeatureLayer As IFeatureClass
pFeatureLayer =  pFeatureWorkspace.OpenFeatureClass("Feature Class Name Here")
0 Kudos
NehayaArabeiat
New Contributor
Hi James,

I'm trying to use the function in the previous thread but it displays this error:
Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Carto.IFeatureLayer'
because the function needs featurelayer parameter and I have accessed to it featureclass, I cannot find a method to convert the featureclass to featurelayer

Here is the function code and button code:

Public Sub Sort_Unique(ByVal pLayer As IFeatureLayer, ByVal fieldname As String, ByVal condition As IQueryFilter)

        Dim pTable As ITable
        Dim pTableSort As ITableSort
        Dim sFieldName As String = fieldname
        Dim pCursor As ICursor

        Dim pFeatureLayer As IFeatureLayer
        pFeatureLayer = New FeatureLayer

        ' Gets the attribute table from the passed layer
        pTable = pLayer.FeatureClass

        ' This example sorts the specificed field name
        pTableSort = New TableSort
        With pTableSort
            .Fields = sFieldName
            .Ascending(sFieldName) = True
            .Table = pTable
        End With

        ' sort the table
        pTableSort.Sort(Nothing)

        ' Loop through sorted records and add to a listbox
        pCursor = pTableSort.Rows

        Dim pData As IDataStatistics
        pData = New DataStatistics
        pData.Field = fieldname
        pCursor = pLayer.Search(condition, False)
        pData.Cursor = pCursor
        Dim pEnumVar As System.Collections.IEnumerator
        Dim value As Object

        pEnumVar = pData.UniqueValues
        value = pEnumVar.MoveNext

        For i = 0 To pData.UniqueValueCount - 1
            Dist_List.Items.Add(CStr(pEnumVar.Current)) '<--- this should add the values to your cmbLot Control
            value = pEnumVar.MoveNext
        Next
    End Sub

    Private Sub Fill_Dist_List_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fill_Dist_List.Click

        Dim pGDBWSF As IWorkspaceFactory
        pGDBWSF = New FileGDBWorkspaceFactory

        Dim pWorkSpace As IWorkspace
        pWorkSpace = pGDBWSF.OpenFromFile("E:\ArcGis Engine Projects\Data Layers\SDE GDB\sde_gdb.gdb", 0)

        Dim pEnumDataset As IEnumDataset
     
        pEnumDataset = pWorkSpace.Datasets(esriDatasetType.esriDTFeatureDataset)

     
        Dim pDataset As IDataset
      
        pDataset = pEnumDataset.Next

        Dim FCN As String
        FCN = Gov_List.SelectedText

        Dim pFeatureworkspace As IFeatureWorkspace
        pFeatureworkspace = pGDBWSF.OpenFromFile("E:\ArcGis Engine Projects\Data Layers\SDE GDB\sde_gdb.gdb", 0)

        Dim pFeatureclass As IFeatureClass
        pFeatureclass = pFeatureworkspace.OpenFeatureClass("amman_dist")
        Dim pFeaturelayer As IFeatureLayer
        'pFeaturelayer = pFeatureclass
        Dim Fieldname As String
        Fieldname = "DIST_NA"
        Dim pQFilter As IQueryFilter
        pQFilter = New QueryFilter
        pQFilter.WhereClause = "DIST_NA = 'ا�?جا�?عة' OR DIST_NA = 'ا�?ج�?زة' OR DIST_NA = 'ا�?ج�?ز�?' OR DIST_NA 'ا�?�?�?�?س�?�?'"

        Sort_Unique(pFeatureclass, Fieldname, pQFilter)

    End Sub


Thanks alot
0 Kudos
JamesCrandall
MVP Frequent Contributor
Untested and I cannot remember exactly, but I think you need something like this:

Set pFeatureLayer = New FeatureLayer
Set pFeatureLayer.FeatureClass = pFeatureClass 


So in the click event that sends the pFeatureLayer to your Sort_Unique Sub, you can try to set your pFeatureLayer as below.  (You'll have to test this as I am away from my dev workstation and just going off of a guess):



   
Private Sub Fill_Dist_List_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fill_Dist_List.Click

        Dim pGDBWSF As IWorkspaceFactory
        pGDBWSF = New FileGDBWorkspaceFactory

        Dim pWorkSpace As IWorkspace
        pWorkSpace = pGDBWSF.OpenFromFile("E:\ArcGis Engine Projects\Data Layers\SDE GDB\sde_gdb.gdb", 0)

        Dim pEnumDataset As IEnumDataset
      
        pEnumDataset = pWorkSpace.Datasets(esriDatasetType.esriDTFeatureDataset)

      
        Dim pDataset As IDataset
       
        pDataset = pEnumDataset.Next

        Dim FCN As String
        FCN = Gov_List.SelectedText

        Dim pFeatureworkspace As IFeatureWorkspace
        pFeatureworkspace = pGDBWSF.OpenFromFile("E:\ArcGis Engine Projects\Data Layers\SDE GDB\sde_gdb.gdb", 0)

        Dim pFeatureclass As IFeatureClass
        pFeatureclass = pFeatureworkspace.OpenFeatureClass("amman_dist")

        Dim pFeaturelayer As IFeatureLayer = New FeautureLayer 
        pFeaturelayer.FeatureClass = pFeatureclass 

        Dim Fieldname As String
        Fieldname = "DIST_NA"
        Dim pQFilter As IQueryFilter
        pQFilter = New QueryFilter
        pQFilter.WhereClause = "DIST_NA = 'ا�?جا�?عة' OR DIST_NA = 'ا�?ج�?زة' OR DIST_NA = 'ا�?ج�?ز�?' OR DIST_NA 'ا�?�?�?�?س�?�?'"

        Sort_Unique(pFeatureclass, Fieldname, pQFilter)

    End Sub


Thanks alot
0 Kudos