Results 1 to 3 of 3

Thread: Populating DataGridView ComboBox column during runtime

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Location
    Tennessee
    Posts
    130

    Populating DataGridView ComboBox column during runtime

    I've got a window with multiple datagridviews. Each with data from a separate dataTable. All of which are part of the same DataSet. There are a couple of places where one DataTable has a field joined with the lookup dataTable from the dataset. For example a lookup table for LugSizes has a field called LugSize that is also it's primary key. Another table called Device Terminations has a field called LugSize to which the user can only select those values from the LugSize lookup table. I thought it would be as simple as using the Items.Add method for the combobox column whenever a new lookup table row was added to its own datagridview. Unfortunately, when I try to initially clear the items list, the data error event is triggered for every row. Is this the wrong approach?

    Here's a sample of my code if it helps:

    HTML Code:
        Private Sub FillLugSizeComboBox()
            Try
    
                If colDTLugSize.Items.Count > 0 Then colDTLugSize.Items.Clear()
                If colTBLugSize.Items.Count > 0 Then colTBLugSize.Items.Clear()
                For Each recLugDim As DataRow In dbSettings.LugDimension.Table.Rows
                    colDTLugSize.Items.Add(recLugDim.Item("LugSize"))
                    colTBLugSize.Items.Add(recLugDim.Item("LugSize"))
                Next
                dgvDeviceTermDef.Refresh()
                dgvTBPanelDef.Refresh()
    
            Catch ex As System.Exception
    
                MessageBox.Show("Error Filling LugSize ComboBox" & vbCrLf & Err.Source & vbCrLf & _
                                "Error Number: " & Err.Number & vbCrLf & ex.Message, _
                                "WireShopAutomation: Advanced Options Dialog", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
            End Try
        End Sub
    
       Private Sub dgvLugDimensions_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvLugDimensions.CurrentCellChanged
    
    
            If dgvLugDimensions.IsCurrentRowDirty Then
                dgvLugDimensions.BindingContext(dbSettings.LugDimension.Table).EndCurrentEdit()
    
                FillLugSizeComboBox()
    
                btnApply.Enabled = True
            End If
    
        End Sub
    Any help would be appreciated.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Re: Populating DataGridView ComboBox column during runtime

    Why don't you bind the lookup table to the combo box in the dgv. So when the lookup table is updated, then these changes will be mirrored in the combo box.

    Computerman
    It was much easier in VB6, but I am now liking Vb.Net alot more.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Location
    Tennessee
    Posts
    130

    Re: Populating DataGridView ComboBox column during runtime

    I don't know why I didn't look at that at first, but in the end that's what I've done. It turns out that the reason for the error is that the Items.Clear statement creates a situation where the values in the list do not match the corresponding field values.

    It still seems like there ought to be a way to make this work.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width