Read shapefile Attributes to combobox in datagridview

SigsterSal

New member
Joined
Oct 7, 2024
Messages
4
Programming Experience
Beginner
Hi
I am reading shapfile name to Columns and Attributes to combobox in datagridview Columns(2)
but I get Attributes from all shape to each combobox
don't separate each combobox with just Attributes from each file individually


VB.NET:
 Dim tableNames As New List(Of String)()
        Dim openFileDialog As New OpenFileDialog()

        ' Configure OpenFileDialog to select multiple .shp files
        openFileDialog.Filter = "Shapefiles (*.shp)|*.shp"
        openFileDialog.Multiselect = True
        openFileDialog.Title = "Select Shapefiles"

        ' Show the dialog and check if the user selected files
        If openFileDialog.ShowDialog() = DialogResult.OK Then
            ' Loop through each selected .shp file
            For Each shapefilePath In openFileDialog.FileNames
                ' Add the file name to Column 1
                Dim rowIndex As Integer = DataGridView1.Rows.Add()
                DataGridView1.Rows(rowIndex).Cells(1).Value = System.IO.Path.GetFileName(shapefilePath)


                ' Read features from the shapefile
                For Each feature In Shapefile.ReadAllFeatures(shapefilePath)
                    ' Loop through attributes and add non-empty values to tableNames
                    For Each attrName In feature.Attributes.GetNames()

                        If Not String.IsNullOrEmpty(attrName) Then
                            Dim attrValue As String = feature.Attributes(attrName)?.ToString()
                            If Not String.IsNullOrEmpty(attrValue) Then
                                tableNames.Add(attrValue)
                            End If
                        End If
                    Next

                Next

                ' Populate the combobox for table names in Column 2
                Dim comboColumn1 As DataGridViewComboBoxColumn = DirectCast(DataGridView1.Columns(2), DataGridViewComboBoxColumn)
                comboColumn1.Items.Clear()
                comboColumn1.Items.AddRange(tableNames.ToArray())

            Next
        End If


Thanks
 
What you should do is bind a full list of possible values to the column, so each cell can have any value from that full list. When the user edits a cell though, you display a filtered list in the editing control and then they will only be able to select from that partial list. You're already doing the first part. For the second part, handle the EditingControlShowing event of the grid and, if it's the right column, get the editing control as a ComboBox and populate it with only the items you want the user to see for the current row.
 
Back
Top